开发者

How can I correctly catch-all subdomains (and redirect) with .htaccess?

I have wild card subdomains (*.domain.com) set up on my server. I now would like to use .htaccess to redirect all *.domain.com requests to a script main.php on my server. I searched for code that would help accomplish the redirect but I have not been entirely successful. The best working code I have found is:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC] 
RewriteRule ^(.*)$ http://domain.com/开发者_StackOverflow中文版%1 [QSA,R=301,L]

However, www.subdomain.domain.com is redirected to domain.com/www.subdomain instead of domain.com/subdomain. How can this be fixed in the code? Is there a better way of doing this?

Thanks in advance!


Can you try this rewrite rule:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)\.domain\.com$ [NC] 
RewriteRule ^(.*)$ http://domain.com/%2 [R=301,L]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜