1

/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]
2
  • I don't see how filename.php would redirect to filename because your rule checks for *index.(php|html)*. What exactly are you trying to accomplish here? Please be more specific, as your question says one thing, but your code says another. Commented Jan 17, 2016 at 5:45
  • The text is what I'm trying to accomplish. The code was my attempt. So far I think all that I've actually accomplished is the removal of the slash. Commented Jan 17, 2016 at 6:18

2 Answers 2

2

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Beautiful. Thank you.
0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.