我应该使用什么RewriteRule(使用.htaccess / mod_rewrite)将http://example.com/blog/(带或不带www)重定向到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指令来检查主机名,只需确保规则位于www.example.com的正确VirtualHost中.
<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实际上是独立的虚拟主机)