【问题标题】:Rewrite URL from http://example.com/blog/ to http://blog.example.com/将 URL 从 http://example.com/blog/ 重写为 http://blog.example.com/
【发布时间】:2009-04-17 07:25:14
【问题描述】:

我应该使用什么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]

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    在您现有的规则中,您似乎有一些错误的方法,我认为没有必要进行否定(即!)测试。

    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.comwww.example.com 实际上是独立的虚拟主机)

    【讨论】:

      猜你喜欢
      • 2012-03-03
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 2013-03-02
      • 2011-03-23
      • 2015-05-21
      • 1970-01-01
      相关资源
      最近更新 更多