【问题标题】:Apache canonical url 301 redirect and friendly url rewriting problemApache 规范 url 301 重定向和友好 url 重写问题
【发布时间】:2011-05-07 22:13:01
【问题描述】:

我有以下问题:

我想将非规范的 url 重定向到规范的 URL,但要保持友好的 url 重写。

在我的 .htaccess 中,我有以下内容:


RewriteEngine On

#Protect some directories
RewriteRule ^(system|modules|application) - [F,L]

#Force Using Canonical Name
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .* http://www.example.com/$0 [R=301]

#Friendly url rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?route=$0 [QSA,L]

我对这个地址没有任何问题:

http://www.example.com/articles/10/this-is-a-sample-post.html

但是当我尝试访问没有“www”的相同网址时。部分, 它被重定向到:

http://www.example.com/index.php?route=articles/10/this-is-a-sample-post.html

换句话说,

http://example.com/articles/10/this-is-a-sample-post.html

必须重定向到

http://www.example.com/articles/10/this-is-a-sample-post.html

有人知道问题出在哪里吗?谢谢

【问题讨论】:

    标签: mod-rewrite redirect apache2


    【解决方案1】:

    问题在于您的外部重定向需要首先执行,但目前它实际上发生在您最后一个规则块之后。添加L flag 以强制立即重定向将解决您的问题:

    RewriteRule .* http://www.example.com/$0 [R=301,L]
    

    没有该标志,在重定向发生之前,第二个规则块将再次转换 URL,这就是您得到意外行为的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 2014-11-13
      • 2014-02-24
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      相关资源
      最近更新 更多