开发者

htaccess subdomai (part 2)

Ok after my previous post, i have made some progress, yet still i need help. For those that read this post first, here is a synopsis.

Ok, project wants each registered member to have a unique url (subdomain), for example user foobar will have foobar.example.com.

After some reading i have asked my hosting company to enabled name server and web server to accept wildcard requests.

This part of the project i think i have managed to solved/create. Now i need to find a way to create subpages in subdomains.

Purpose is demo.example.co开发者_StackOverflow社区m and demo.example.com/ and demo.example.com/index.php to be served by www.example.com/sub_index.php?user=demo

demo.example.com/gallery.php to be served by www.example.com/sub_gallery.php?user=demo

demo.example.com/gallery.php?page=2 to be served by www.example.com/sub_gallery.php?user=demo&page=2

All pages in subdomains have the same pattern: [sub_].

Based on the above, i have constructed some htaccess rules. But I get no good results.

Actually: requests made to demo.example.com work perfect requests made to demo.example.com/ OR demo.example.com/index.php show me the example.com/index.php requests to demo.example.com/gallery.php throws 404 error (although page sub_gallery.php is in root folder).

My current htaccess (at root folder) is:

Options +FollowSymLinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /

#rewrite example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.htm([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]



###########################
##  subdomains
###########################
    #index
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^$ /sub_index.php?user=%1 [L]

    #other pages
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^/other_page.php$ /sub_other_page.php?user=%1 [QSA,L]

Can anyone see any error and provide some feedback? Thank you


Try updating your last rule to the following. It is dynamic for the requested page, gallery.php -> sub_gallery.php, and I removed the leading /, as that is not present when rewriting from the .htaccess file.

RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^([^\.]*).php$ /sub_$1.php?user=%1 [QSA,L,R]

Hopefully this helps.


I just thought to post here the final code, for future readers

#######################################################
##### rewrite example.com into www.example.com #####
######################################################
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

###########################
## subdomains
###########################
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^([^\.]*)\.php$ /sub_$1.php?user=%1 [QSA,L]

This code will take any page named: sub_[PAGE].php?user=demo&id=5 and create demo.example.com/[PAGE].php?id=5

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜