Home > OS >  how to exclude .html and add / at the end of the url in htaccess
how to exclude .html and add / at the end of the url in htaccess

Time:12-25

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]

CodePudding user response:

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]
  • Related