重写URL到http://blog.example.com/
问题描述:
什么RewriteRule
(使用.htaccess
/mod_rewrite
)我应该使用重定向http://example.com/blog/
(与www
或不)到http://blog.example.com/
?重写URL到http://blog.example.com/
我用以下,但得到重定向循环:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/blog/ [L,R=301]
RewriteCond %{HTTP_HOST} www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/blog/ [L,R=301]
答
在现有的规则,你似乎有一些东西,周围的错误的方式,我不认为有任何需要的负面(即!
)测试。
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^/blog/$ http://blog.example.com/ [L,R=301]
不过我建议你不要使用RewriteCond
指令,检查主机,只需确保规则是在正确的VirtualHost
为www.example.com
。
<VirtualHost ...>
ServerName www.example.com
ServerAlias example.com
RewriteRule ^/blog/ http://blog.example.com/ [L,R=301]
</VirtualHost>
(NB:假定blog.example.com
和www.example.com
实际上是单独的虚拟主机)