什么 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
检查主机名的指令,只需确保规则在正确的 VirtualHost
中为 www.example.com
.<VirtualHost ...>
ServerName www.example.com
ServerAlias example.com
RewriteRule ^/blog/ http://blog.example.com/ [L,R=301]
</VirtualHost>
(注意:假设
blog.example.com
和 www.example.com
实际上是独立的虚拟主机)
关于regex - 将 URL 从 http ://example. com/blog/重写为 http ://blog. example.com/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/759361/