Php Htaccess:从URL添加/删除尾部斜杠

Php Htaccess:从URL添加/删除尾部斜杠,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我的网站运行一个名为->WSS墙纸脚本的脚本 我的问题->我一直试图强制删除或在我的URL末尾添加尾随斜杠,以防止重复内容,并清理我的URL 我尝试了各种各样的方法,尝试了所有我能想到的方法,并且从互联网上下载了很多东西,但是还没有这样的运气!这可能是一个快速解决办法,但我已经看了这么多,我可能对一些非常明显的东西视而不见 因此,我向您展示我所有的.htaccess代码: DirectoryIndex index.php RewriteEngine on RewriteRule ^downlo

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

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

我尝试了各种各样的方法,尝试了所有我能想到的方法,并且从互联网上下载了很多东西,但是还没有这样的运气!这可能是一个快速解决办法,但我已经看了这么多,我可能对一些非常明显的东西视而不见

因此,我向您展示我所有的
.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 ##

在行的
重写引擎的正下方,添加:

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

RewriteRule^(.*)/$/$1[L,R]#为了补充Jon Lin的答案,这里有一个无尾随斜杠技术,如果网站位于目录(如example.org/blog/)中,它也可以工作:


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

RewriteCond%{REQUEST\u FILENAME}-D
RewriteCond%{REQUEST_URI}/(.*)/$
重写规则^/%1[R=301,L]
Options+FollowSymLinks
重新启动发动机
重写基/
##hide.html扩展名
#从外部将/dir/foo.html重定向到/dir/foo
重写cond%{THE_REQUEST}^[A-Z]{3,}\s([^.]+).html
重写规则^1[R=301,L]
重写cond%{u请求}^[A-Z]{3,}\s([^.]+)/\s
重写规则^1[R=301,L]
##在内部将/dir/foo重定向到/dir/foo.html
RewriteCond%{REQUEST_FILENAME}.html-f
重写规则^([^\.]+)$$1.html[L]
命令允许,拒绝
全盘否定
满足所有

这将删除html代码或php(如果您对其进行了补充)。允许您添加尾随斜杠,它会出现,以及没有尾随斜杠的url都绕过404代码。再加上一点额外的安全性

这是我在最新应用程序中使用的

# 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

另外,如果有人能修复这个坏男孩,你能在这里发布回复吗?谢谢。请在这里发布代码。编辑你的帖子,将代码粘贴到帖子中,高亮显示,按“CTRL+K”将其格式化为代码。你真的有吗?你的URI允许吗?其中一条规则(接近底部)表明了这一点。我担心他们可能会混淆服务器甚至之前。htaccess可以处理他们。上帝,我可以吻你!与强制执行无尾随斜杠一起工作另一个给了我500服务器错误谢谢Jon Lin!如果网站位于某个目录(如example.org/blog/)中,则无尾随斜杠规则不起作用。请小心
R=301
!有了它,许多浏览器都会无限期地缓存.htaccess文件:如果无法在所有打开它的计算机上清除浏览器缓存,那么它就会变得不可逆转。测试时,最好使用简单的“R”或“R=302”,当VirtualDocumentRoot处于禁用状态时,删除开头的斜杠,即:
$1
而不是
/$1
,并且,@JonathanWeber,.htaccess文件不提供,但是expires头告诉浏览器缓存资源的时间。@Nrc,因为除了文件和目录之外,还有其他类型的映射,而且请求可能既不映射到文件也不映射到目录,但需要重写。负数确保跳过已重写或已映射到文件系统中的内容。此特定变体非常有用,尤其是在使用现代URL约定时。希望你不介意,我将它提交给。这消除了当请求URI(根)被解释为尾随斜杠时的问题,也会导致无限循环。谢谢是的,如果网站位于某个目录中,接受的答案不会处理,但是这个带有
RewriteCond%{REQUEST_URI}(+)/$
的答案只删除了最后一个尾随斜杠(例如,需要从sth///到sth//到sth/到sth的几个重定向)。要在一个重定向中删除所有尾部斜杠,请使用更好的解决方案
RewriteCond%{REQUEST_URI}(.+?)/+$
,我也考虑过这一点。不过,结果是正确的,考虑到这是一种边缘情况,我更喜欢代码的简单性。@GrasDouble,感谢您长期以来一直在寻找这个解决方案。尝试了无数种方法,但你的效果很好!非常感谢。嗨,你如何为nginx写这篇文章?我试图在这里的转换器中运行它:但这个结果给了我无限循环:
#nginx配置位置/{rewrite^(.*)$/%1重定向;rewrite^(.*)$/%1重定向;rewrite^/([^\.]+)$/$1.html中断;}location~^.*([Hh][Tt][Aa]){deny all;}
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L] <-- added slash here too, don't forget it
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>
# 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