我将 mod_rewrite/.htaccess 用于漂亮的 URL。
我正在使用此条件/规则来消除尾部斜杠(或者更确切地说:通过 301 重定向重写为非尾部斜杠 URL;我这样做是为了避免重复内容,因为我喜欢没有尾部斜杠的 URL更好的):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^\.localhost$ [NC]
重写规则 ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
到目前为止运作良好。唯一的缺点:
它还转发“multiple-trailing-slash”-URLs 到non-trailing-slash-URLs。
例子:http://example.tld/foo/bar//////
转发至 http://example.tld/foo/bar
而我只想要 http://example.tld/foo/bar/
转发到http://example.tld/foo/bar
.
那么,如果它实际上只是一个尾部斜杠,是否可以只消除尾部斜杠?
对不起,如果这是一个有点烦人或奇怪的问题!
谢谢。
最佳答案
以下规则将匹配以斜杠结尾的任何 URL,并从其末尾删除所有斜杠:
RewriteRule ^(.*)/+$ $1 [R=301,L]
注意:当前接受的答案仅适用于 http 而不是 https,但此答案对两者都适用。
关于apache - mod_rewrite : remove trailing slash (only one! ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3068091/