[英]Removing trailing slash AND php/html extension. Trailing slash scenario fails to remove extension
/filename.php becomes /filename - Fail /filename.php变为/ filename-失败
/filename.php/ becomes /filename.php - Fail! /filename.php/变成/filename.php-失败!
/filename/ becomes filename - success! / filename /成为文件名-成功!
How would I remove the extension on the scenario that has the trailing slash? 在带有尾部斜杠的方案中,如何删除扩展名?
Options +MultiViews
RewriteEngine On
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
# remove php/html extension
RewriteCond %{THE_REQUEST} /index\.(php|html)[\s/?] [NC]
RewriteCond %{REQUEST_URI} !/system/ [NC]
RewriteRule ^(.*?)index\.(?:php|html)(.*)$ $1$2 [R=301,NE,L]
You can use these 2 rules: 您可以使用以下2条规则:
# externally redirect /dir/file.php to /dir/file and remove index
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:html?|php)/?[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=302,L]
You'd be better off with this: 您最好这样做:
RewriteEngine On
# Trim trailing slash (only if request is not for a directory)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Trim .php/.html from request (exclude requests to /system/*)
# Ex: /page.php -> /page
RewriteCond %{REQUEST_URI} !/system/ [NC]
RewriteRule ^(.+).(?:php|html)$ /$1 [L,R=301]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.