Ok, purpose is to have unlimited number of subdomains, created dynamically for our members. So, a request to www.example.com/sub_index.php?user=demo will translate to demo.example.com url, while www.example.com is served by index.php.
I have enabled name 开发者_如何学Cserver and web server to accept wildcard requests (actually i contact my hosting server to do that).
On the htaccess part i have the following:
####rewrite example.com into 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
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^(.*)$ sub_index.php?user=%1
So, a request to demo.example.com/ and to demo.example.com/index.php is served by the www.example.com/sub_index.php?user=demo
My problem is that i cant find a way to have subpages work in the subdomain For example: demo.example.com/somepage.php should be served by www.example.com/sub_page.php?user=demo while demo.example.com/otherpage.php?id=5 should be servered by www.example.com/sub_page2.php?user=demo&id=5.
Thank you for any advice/tip
To keep your rewriting rules manageable, you're best bet is to transform urls without introducing new variables.
With your example, it is impossible to use a simple textual replacement to get from somepage.php to sub_page.php and then otherpage.php to sub_page2.php - you will need a new rule for each page, because only you know about the mapping. Its a lot easier of you base the result url on data in the source url.
For example, you could devise a scheme whereby:
demo.example.com/somepage.php?start=5
transforms into
www.example.com/sub_somepage.php?user=demo&start=5
With this scheme, the name of the target script can be derived from the name of the original script. The RewriteRule for this scheme would look like:
RewriteRule ^/([A-Za-z0-9_]+\.php) /sub_$1?user=%1 [qsappend]
Getting rewrite rules to work correctly can be quite tricky. While you refine your rules, you will probably want to add this to your config:
RewriteLog "/tmp/httpd-rewrite.log"
RewriteLogLevel 5
You can then check that your rules are doing what you want. More information on how these directives work is at httpd.apache.org. In particular, if you want to redirect to a different host to serve content, you'll have to add the new host name to the substitution and the [redirect] flag.
精彩评论