Ad

How To Exclude .html And Add / At The End Of The Url In Htaccess

- 1 answer

i have this htaccess code that removes the .html extension for my URL, but i want to add / at the end of the URL after the .html extension is removed, how to i do that? here is my code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
Ad

Answer

You can use this

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/?$ $1.html [NC,L]

However this rule doesn't remove .html extension from URLs , it just makes it so that you can type exampe.com/file instead of example.com/file.html in browser address bar. If you want to remove html extension from URLs you can use the following complete rule :

RewriteEngine On
#redirect /file.html to /file
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ %1/ [L,R=301]
#internally map /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/?$ $1.html [NC,L]
Ad
source: stackoverflow.com
Ad