【问题标题】:.htaccess 301 redirect if page has a trailing slash如果页面有斜杠,.htaccess 301 重定向
【发布时间】:2014-11-25 23:06:05
【问题描述】:

可以说我的域页面是 www.website.com/about 然后用户放 www.website.com/about/(尾部斜杠)在末尾​​p>

它返回相同的页面,太棒了。但是,搜索引擎可能会将其视为重复内容,因为它可能被视为 2 页。

如果用户在页面末尾添加斜杠,我希望他们执行 301 重定向到正确的页面。

我的 .htaccess 文件看起来像这样

    # Begin cache control #
    ExpiresActive on
    ExpiresDefault "now plus 1440 minutes"
    ExpiresByType text/html "now plus 1440 minutes"
    <FilesMatch "\.(css|png|bmp|ico|htm|gff|html|js|jpg|jpeg|gif|gcf)$">
    FileETag MTime Size
    ExpiresDefault "now plus 1440 minutes"
    </FilesMatch>


    # End cache control #
    Options +FollowSymLinks -MultiViews


    AddOutputFilterByType DEFLATE text/html image/png image/jpeg image/jpg text/css text/javascript 
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    ## hide .php extension
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R,L,NC]
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.*?)/?$ $1.php [L,NC]

有谁知道我如何实现这一点,因为我尝试的一切都行不通。

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    您可以在最后一条规则之前插入此规则以删除尾部斜杠:

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

    【讨论】:

      猜你喜欢
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 2013-05-10
      相关资源
      最近更新 更多