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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Htaccess:从URL添加/删除尾部斜杠相关的知识,希望对你有一定的参考价值。

我的网站运行一个名为 - > 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 ##
答案

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

另一答案

为了补充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

另一答案

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

# 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
另一答案
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代码。再加上一点安全性。

以上是关于Htaccess:从URL添加/删除尾部斜杠的主要内容,如果未能解决你的问题,请参考以下文章

htaccess:从以.xml / only结尾的URL中删除尾部斜杠

从 Magento URL 中删除尾部斜杠

mod_rewrite:删除尾部斜杠(只有一个!)

删除htaccess中的尾部斜杠

通过 .htaccess 从 URL 中删除双或更多斜杠的问题

从 JAVA 中的字符串(从 url 类型更改)中删除尾部斜杠