开发者

Htaccess help - dynamic subdomains

I have searched a lot for this, and tried a lot of things I found on the web, but noting has worked for me.


How I can make, when the user开发者_JAVA技巧 visit this page:

http://subdomain.domain.com/

It shows him this page (not redirect):

http://www.domain.com/?x=subdomain

And when the user visit this page:

http://subdomain.domain.com/number/

It shows him this page (not redirect):

http://www.domain.com/?x=subdomain&y=number

And when the user visit this page:

http://subdomain.domain.com/string/number/

It shows him this page (not redirect):

http://www.domain.com/?x=subdomain&z=string&y=number

Also, subdomain value change and number value change

More info: Apache/2.2.16 (Unix) mod_ssl/2.2.16 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635


Please explain the code if you can...


Thank you :)


This looks a lot like a code request, and not a question, but I'm feeling nice today. So here you have it.

RewriteEngine On
RewriteRule ^index2\.php - [L]
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteCond %{HTTP_HOST} !^(www\.)example\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example.com [NC]
RewriteRule ^([^/]+/)?([^/]+/)? index2.php?x=%2&y$1&z=$2 [L,QSA]

the index2.php is needed, because the rewrite would be stuck in a loop otherwise (You could just include index.php inside it, so no need to copy code). This is what line 2 is for. If it detects the rewriting is already done it will stop there. Line 3 makes sure the rewrite doesn't occur on the main domain. Line 4 takes the subdomainname part (www. optional) and puts it in %2. Line 5 pick the optional first and second folder and uses these combined with the subdomainname to create you expected url. Note that the '/' is included in the y and z, because otherwise the rule would have to many parentheses to understand easily.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜