【问题标题】:Removing URL Trailing Slash [duplicate]删除 URL 尾部斜杠 [重复]
【发布时间】:2016-01-10 22:37:17
【问题描述】:

我建立了一个博客(我希望今天能上线),但最近遇到了 .htaccess 重写以实现更好的 url 重定向,但我遇到了一些问题。我已经使用RewriteRule 整理了实际的 Rewrite,但后来遇到了“尾斜线与无尾斜线”的问题。

我决定使用 无尾随斜杠,但我认为因为 google 将它们都视为唯一地址,所以我最好使用某种重定向,以便从 url 中删除 /

这是我目前拥有的htaccess 代码:

Options -MultiViews

DirectoryIndex articles.php

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d    

RewriteRule ^articles/([\w-]+)/?$               articles.php?cat=$1 [NC,L,QSA]
RewriteRule ^articles/([\w-]+)/([0-9]+)/?$      articles.php?cat=$1&currentpage=$2 [NC,L,QSA]
RewriteRule ^article/([0-9]+)/([\w-]+)/?$       article.php?id=$1&title=$2 [NC,L,QSA]

RewriteRule ^articles articles.php
RewriteRule ^articles/ articles [R=301,L] /* Problem Line */

除了最后一行之外,上述所有代码一切正常,因为当我在 URL 中添加斜杠时,它不会重定向和删除斜杠。如何实现?是否有解决方法可以自动删除 any 页面上的尾部斜杠?

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    这样吧:

    Options -MultiViews
    DirectoryIndex articles.php
    RewriteEngine On
    
    ## Unless directory, remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+?)/$ /$1 [NE,R=302,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d    
    RewriteRule ^ - [L]
    
    RewriteRule ^articles/([\w-]+)/?$  articles.php?cat=$1 [NC,L,QSA]
    RewriteRule ^articles/([\w-]+)/([0-9]+)/?$ articles.php?cat=$1&currentpage=$2 [NC,L,QSA]
    RewriteRule ^article/([0-9]+)/([\w-]+)/?$ article.php?id=$1&title=$2 [NC,L,QSA]
    RewriteRule ^articles/?$ articles.php [L]
    

    【讨论】:

      猜你喜欢
      • 2012-12-09
      • 2017-05-03
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多