【问题标题】:Htaccess: add/remove trailing slash from URLHtaccess:从 URL 添加/删除尾部斜杠
【发布时间】:2021-12-14 12:45:15
【问题描述】:

我的网站运行一个名为 -> WSS 壁纸脚本的脚本

我的问题 -> 我一直在尝试强制删除或在我的网址末尾添加斜杠,以防止重复内容并清理我的网址。

我已经尝试了各种方法并尝试了所有我能想到的和从互联网加载的东西,但还没有这样的运气!这可能是一个快速的解决方案,但我已经看过太多了,我可能对一些明显的东西视而不见。

所以我向你展示我所有的.htaccess 代码:

DirectoryIndex index.php

RewriteEngine on
RewriteRule ^download/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) image.php?id=$1&width=$2&height=$3&cropratio=$4&download=1 [L]
RewriteRule ^file/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) image.php?id=$1&width=$2&height=$3&cropratio=$4 [L]
RewriteRule ^preview/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) wallpaper_preview.php?id=$1&width=$2&height=$3&name=$4 [L]
RewriteRule ^thumbnail/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/]+) image.php?wallpaper_id=$1&width=$2&height=$3&cropratio=$4&align=$5&valign=$6&file=$7 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/p([0-9]+) index.php?task=category&id=$1&name=$2&page=$3 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/([0-9a-zA-Z?-]+)/p([0-9]+) index.php?task=category&id=$1&name=$2&sortby=$3&page=$4 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/([0-9a-zA-Z?-]+)-([0-9]+) index.php?task=category&id=$1&sortby=$3&page=$4 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+) index.php?task=category&id=$1&name=$2 [L]
RewriteRule ^tag/([^/\.]+)/([0-9a-zA-Z?-]+)/([0-9]+) index.php?task=tag&t=$1&sortby=$2&page=$3 [L]
RewriteRule ^tag/([^/\.]+) index.php?task=tag&t=$1 [L]
RewriteRule ^profile/([0-9]+)?/([^/\.]+) index.php?task=profile&id=$1&name=$2 [L]
RewriteRule ^profile/comments/([0-9]+)?/([^/\.]+) index.php?task=users_comments&id=$1&name=$2 [L]
RewriteRule ^page/([0-9]+) index.php?task=view_page&id=$1 [L]  
RewriteRule ^register index.php?task=register [L] 
RewriteRule ^lost-password index.php?task=lost_pass [L] 
RewriteRule ^links index.php?task=links [L]
RewriteRule ^news/item/([0-9]+)/([^/\.]+) index.php?task=news&id=$1 [L]
RewriteRule ^news/page([0-9]+) index.php?task=news&page=$1 [L]
RewriteRule ^members/([^/\.]+)-([^/\.]+)/page([0-9]+)? index.php?task=member_list&sort=$1&order=$2&page=$3 [L]
RewriteRule ^members index.php?task=member_list [L]
RewriteRule ^messages index.php?task=messages [L]
RewriteRule ^submit index.php?task=submit [L]
RewriteRule ^search/([^/\.]+) index.php?task=search&q=$1 [L]
RewriteRule ^search index.php?task=search [L]
RewriteRule ^submit index.php?task=submit [L]
RewriteRule ^r-([0-9]+)?-([0-9]+)? go.php?id=$1&ref=$2 [L]
RewriteRule ^r-([0-9]+)? go.php?id=$1 [L]
RewriteRule ^([^/\.]+)/([0-9]+)/([^/\.]+) index.php?task=view&id=$2&name=$3 [L]
RewriteRule ^news/([^/\.]+) index.php?task=news&name=$1 [L]
RewriteRule ^profile/([^/\.]+) index.php?task=profile&name=$1 [L]
RewriteRule ^news index.php?task=news [L]
RewriteRule ^page/([^/\.]+) index.php?task=view_page&name=$1 [L]  
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([0-9a-zA-Z'?-]+)/([0-9]+) index.php?task=category&name=$1&sortby=$2&page=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?task=view&name=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+) index.php?task=category&name=$1 [L]


## www reslove ##
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
## www reslove ##

## index reslove ##
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://www.epicwallpaper.net/$1 [R=301,L]
## index reslove ##

【问题讨论】:

  • 另外,如果有人能解决这个坏男孩,你能在这里回复吗?谢谢
  • 请把代码贴在这里。
  • 编辑您的帖子,将您的代码粘贴到您的帖子中,突出显示它,按“CTRL+K”将其格式化为代码
  • 您的 URI 中真的允许 '? 吗? RewriteRules 之一(靠近底部)表明了这一点。我担心它们可能会在 .htaccess 处理它们之前混淆服务器。
  • 要从不指向目录的 URL 中删除尾随,您可以使用此解决方案 helponnet.com/2020/02/20/…

标签: php apache .htaccess mod-rewrite


【解决方案1】:

RewriteEngine On 行的正下方,添加:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]

强制执行无尾随斜杠政策。

要强制执行尾部斜杠政策:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R] # <- for test, for prod use [L,R=301]

编辑:评论了R=301 部分,因为如评论中所述:

小心R=301!将它放在那里会使许多浏览器无限期地缓存 .htaccess 文件:如果您无法清除所有打开它的计算机上的浏览器缓存,它就会以某种方式变得不可逆转。测试时,最好使用简单的RR=302

完成测试后,您可以使用R=301

【讨论】:

  • 天哪,我可以吻你!与强制执行无尾随斜杠一起工作,另一个给了我 500 服务器错误谢谢 Jon Lin!
  • 如果网站位于目录(例如 example.org/blog/)中,则无尾随斜杠规则不起作用
  • 小心R=301!拥有它会使许多浏览器无限期地缓存 .htaccess 文件:如果您无法清除所有打开它的机器上的浏览器缓存,它就会以某种方式变得不可逆转。测试时,最好使用简单的“R”或“R=302”
  • 当 VirtualDocumentRoot 关闭时,删除开头的斜杠,即:$1 而不是 /$1 并且,@JonathanWeber 不提供 .htaccess 文件,但 expires 标头告诉浏览器缓存资源的时间。
  • @Nrc 因为除了文件和目录之外还有其他类型的映射,并且请求也有可能既不映射到文件也不映射到目录但需要重写。否定确保跳过已重写或已映射到文件系统中的某些内容。
【解决方案2】:

为了补充 Jon Lin 的回答,如果网站位于目录中(例如 example.org/blog/),这里有一种无尾斜线技术也适用:

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


为了完整起见,这里有一个替代方案,强调REQUEST_URI 以斜杠开头(至少在.htaccess 文件中):

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L] <-- added slash here too, don't forget it

只是不要使用 %{REQUEST_URI} (.*)/$。因为在根目录中REQUEST_URI 等于/,即前导斜杠,会被误解为尾部斜杠。


如果您有兴趣阅读更多内容:

(更新:此技术现在在 Laravel 5.5 中为 implemented

【讨论】:

  • 这个特殊的变体非常有用,尤其是在使用现代 URL 约定时。希望您不要介意,我将其提交给htaccess snippets repository
  • 这消除了当 REQUEST_URI (root) 被解释为尾部斜杠时也会导致无限循环的问题。谢谢
  • 是的,如果网站位于目录中,则不接受接受的答案,但是RewriteCond %{REQUEST_URI} (.+)/$ 的答案仅删除最后一个斜杠(例如,从 sth/// 到 sth// 需要多个重定向对某事/对某事)。要在一次重定向中删除所有尾部斜杠,请使用更好的解决方案 RewriteCond %{REQUEST_URI} (.+?)/+$
  • 我也想过这个问题。虽然结果是正确的,并且考虑到它是一种边缘情况,但我更喜欢代码简单性。
  • @GrasDouble ,感谢您长期以来一直在寻找此解决方案。尝试了无数方法,但你的工作正常!非常感谢。
【解决方案3】:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).html
RewriteRule ^ %1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)/\s
RewriteRule ^ %1 [R=301,L]

## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ $1.html [L]


<Files ~"^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</Files>

如果您补充它,这将删除 html 代码或 php。允许您添加斜杠,它以及没有斜杠的 url 都会绕过 404 代码。加上一点额外的安全性。

【讨论】:

  • 嗨,你会如何为 nginx 编写这个?我试图在这里的转换器中运行它:winginx.com/en/htaccess 但这个结果给了我无限循环:# nginx configuration location / { rewrite ^(.*)$ /%1 redirect; rewrite ^(.*)$ /%1 redirect; rewrite ^/([^\.]+)$ /$1.html break; } location ~ ^.*\.([Hh][Tt][Aa]) { deny all; }
【解决方案4】:

这是我用于最新应用程序的内容。

# redirect the main page to landing
##RedirectMatch 302 ^/$ /landing

# remove php ext from url
# https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess
RewriteEngine on 

# File exists but has a trailing slash
# https://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)/+$ /$1 [R=302,L,QSA]

# ok. It will still find the file but relative assets won't load
# e.g. page: /landing/  -> assets/js/main.js/main
# that's we have the rules above.
RewriteCond %{REQUEST_FILENAME} !\.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^/?(.*?)/?$ $1.php

【讨论】:

    猜你喜欢
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多