php Htaccess:从 URL 添加/删除尾部斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21417263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Htaccess: add/remove trailing slash from URL
提问by Luke
My website runs a script called -> WSS wallpaper script
我的网站运行一个名为 -> WSS 壁纸脚本的脚本
My Problem-> I have been trying to force remove or add trailing slash to the end of my URL to prevent duplicated content and also to clean up my URLs.
我的问题-> 我一直在尝试强制删除或在我的 URL 末尾添加斜杠以防止重复的内容并清理我的 URL。
I have tried all sorts and tried everything I could think of and loads from the interwebs but no such luck yet! It might be a quick fix but I have looked at it so much I am probably blind to something dead obvious.
我已经尝试了各种方法并尝试了我能想到的所有方法并从互联网上加载,但还没有这样的运气!这可能是一个快速解决方案,但我已经看了很多,我可能对一些非常明显的东西视而不见。
So I present you with all my .htaccesscode:
所以我向你展示我所有的.htaccess代码:
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^download/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) image.php?id=&width=&height=&cropratio=&download=1 [L]
RewriteRule ^file/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) image.php?id=&width=&height=&cropratio= [L]
RewriteRule ^preview/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) wallpaper_preview.php?id=&width=&height=&name= [L]
RewriteRule ^thumbnail/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/]+) image.php?wallpaper_id=&width=&height=&cropratio=&align=&valign=&file= [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/p([0-9]+) index.php?task=category&id=&name=&page= [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/([0-9a-zA-Z?-]+)/p([0-9]+) index.php?task=category&id=&name=&sortby=&page= [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/([0-9a-zA-Z?-]+)-([0-9]+) index.php?task=category&id=&sortby=&page= [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+) index.php?task=category&id=&name= [L]
RewriteRule ^tag/([^/\.]+)/([0-9a-zA-Z?-]+)/([0-9]+) index.php?task=tag&t=&sortby=&page= [L]
RewriteRule ^tag/([^/\.]+) index.php?task=tag&t= [L]
RewriteRule ^profile/([0-9]+)?/([^/\.]+) index.php?task=profile&id=&name= [L]
RewriteRule ^profile/comments/([0-9]+)?/([^/\.]+) index.php?task=users_comments&id=&name= [L]
RewriteRule ^page/([0-9]+) index.php?task=view_page&id= [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= [L]
RewriteRule ^news/page([0-9]+) index.php?task=news&page= [L]
RewriteRule ^members/([^/\.]+)-([^/\.]+)/page([0-9]+)? index.php?task=member_list&sort=&order=&page= [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= [L]
RewriteRule ^search index.php?task=search [L]
RewriteRule ^submit index.php?task=submit [L]
RewriteRule ^r-([0-9]+)?-([0-9]+)? go.php?id=&ref= [L]
RewriteRule ^r-([0-9]+)? go.php?id= [L]
RewriteRule ^([^/\.]+)/([0-9]+)/([^/\.]+) index.php?task=view&id=&name= [L]
RewriteRule ^news/([^/\.]+) index.php?task=news&name= [L]
RewriteRule ^profile/([^/\.]+) index.php?task=profile&name= [L]
RewriteRule ^news index.php?task=news [L]
RewriteRule ^page/([^/\.]+) index.php?task=view_page&name= [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([0-9a-zA-Z'?-]+)/([0-9]+) index.php?task=category&name=&sortby=&page= [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?task=view&name= [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+) index.php?task=category&name= [L]
## www reslove ##
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]
## www reslove ##
## index reslove ##
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://www.epicwallpaper.net/ [R=301,L]
## index reslove ##
回答by Jon Lin
Right below the RewriteEngine Online, add:
在该RewriteEngine On行的正下方,添加:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R] # <- for test, for prod use [L,R=301]
to enforce a no-trailing-slash policy.
强制执行无尾随斜线政策。
To enforce a trailing-slash policy:
要强制执行尾部斜杠策略:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ // [L,R] # <- for test, for prod use [L,R=301]
EDIT: commented the R=301parts because, as explained in a comment:
编辑:评论R=301部分,因为,如评论中所述:
Be careful with that
R=301! Having it there makes many browsers cache the .htaccess-file indefinitely: It somehow becomes irreversible if you can't clear the browser-cache on all machines that opened it. When testing, better go with simpleRorR=302
小心点
R=301!拥有它会使许多浏览器无限期地缓存 .htaccess 文件:如果您无法清除所有打开它的机器上的浏览器缓存,它就会以某种方式变得不可逆转。测试时,最好使用简单R或R=302
After you've completed your tests, you can use R=301.
完成测试后,您可以使用R=301.
回答by Gras Double
To complement Jon Lin's answer, here is a no-trailing-slash technique that also works if the website is located in a directory (like example.org/blog/):
为了补充 Jon Lin 的回答,这里有一种无斜杠技术,如果网站位于目录中(如 example.org/blog/)也适用:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
For the sake of completeness, here is an alternative emphasizing that REQUEST_URIstarts with a slash (at least in .htaccessfiles):
为了完整起见,这里有一个REQUEST_URI以斜杠开头的替代强调(至少在.htaccess文件中):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L] <-- added slash here too, don't forget it
Just don't use%{REQUEST_URI} (.*)/$. Because in the root directory REQUEST_URIequals /, the leading slash, and it would be misinterpreted as a trailing slash.
只是不要使用%{REQUEST_URI} (.*)/$. 因为在根目录中REQUEST_URIequals/是前导斜杠,它会被误解为尾随斜杠。
If you are interested in more reading:
如果您有兴趣阅读更多:
(update: this technique is now implementedin Laravel 5.5)
(更新:此技术现已在 Laravel 5.5 中实现)
回答by Svetoslav Marinov
This is what I've used for my latest app.
这是我用于我的最新应用程序的内容。
# 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 ^/?(.*)/+$ / [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 ^/?(.*?)/?$ .php
回答by Scout Hynson
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 ^([^\.]+)$ .html [L]
<Files ~"^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</Files>
This removes html code or php if you supplement it. Allows you to add trailing slash and it come up as well as the url without the trailing slash all bypassing the 404 code. Plus a little added security.
如果您补充它,这将删除 html 代码或 php。允许您添加尾部斜杠,它出现以及没有尾部斜杠的 url 都绕过 404 代码。加上一点额外的安全性。

