删除尾部斜杠和php / html扩展名。尾部斜杠场景无法删除扩展名

时间:2016-01-17 05:19:56

标签: php .htaccess

/filename.php变为/ filename - 失败

/filename.php/成为/filename.php - 失败!

/ filename /成为文件名 - 成功!

如何删除具有尾部斜杠的方案的扩展名?

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]

2 个答案:

答案 0 :(得分: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]

答案 1 :(得分:0)

你最好还是这样:

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]