正则表达式 – 将网址从http://example.com/blog/重写为http://blog.example.com/

我应该使用什么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实际上是独立的虚拟主机)

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...