简体   繁体   中英

Removing trailing slash AND php/html extension. Trailing slash scenario fails to remove extension

/filename.php becomes /filename - Fail

/filename.php/ becomes /filename.php - Fail!

/filename/ becomes filename - success!

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:

# 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM