【问题标题】:How to redirect 301 with dynamically generated URL?如何使用动态生成的 URL 重定向 301?
【发布时间】:2014-09-24 09:56:50
【问题描述】:

在你说做一些研究之前,我知道如何使用 301 重定向。

不过,这是一个非常具体的问题。

所以我有一个有效的 Mod 重写:

RewriteRule ^/news/article/([^/]*)/$ /news/article.php?title=$1 [L]

这会将 url 输出到 /news/article/title/ ,但是我最近想删除尾部斜杠

所以 url 读取 /news/article/title ,很容易做到这一点:

RewriteRule ^/news/article/([^/]*)$ /news/article.php?title=$1 [L]

但是现在当有人进入 /news/article/title/

所以我的问题是有没有办法让我们重定向 301,所以当用户输入 /news/article/title/ 时,他们会被定向到 /news/article/title

请记住,我希望这适用于所有动态生成的 URL..

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    像这样使用R=301 标志:

     RewriteRule ^/news/article/([^/]*)/$ /news/article/$1 [R=301]
     RewriteRule ^/news/article/([^/]*)$ /news/article.php?title=$1 [L]
    

    有关标志的更多详细信息,请参阅apache documentation

    【讨论】:

      【解决方案2】:

      您实际上应该使斜线可选:

      RewriteRule ^/?news/article/([^/]*)/?$ /news/article.php?title=$1 [L,QSA]
      

      要从 URL 中删除尾随 / 斜杠,请使用以下重定向规则:

      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
      RewriteRule ^(.+?)/$ /$1 [R=301,L,NE]
      

      【讨论】:

      • 我真的很喜欢可选的斜线方法!但是删除一个是我觉得最好的解决方案
      猜你喜欢
      • 2015-12-04
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多