Home > Back-end >  Remove .html from website including in subdirectories
Remove .html from website including in subdirectories

Time:09-30

I've tried a lot of the answers here unfortunately I didn't find an answer which worked for me.

I have a website which is in the format of www.website.com/page.html and my goal is to display www.website.com/page whether the user enters www.website.com/page or www.website.com/page.html (SEO reasons as well). So I have the following snippet which works

#1) redirect file.html to file
RewriteCond %{THE_REQUEST} /([^.] ).html [NC]
RewriteRule .  /%1 [L,R=301]
#2) rewrite /file to file.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*)/?$ /$1.html [L]

However I have other URLs which are the format of www.website.com/blog/text.html and the above doesn't work for them. I've tried modifying the above but with no success. My goal is the URL to be www.website.com/blog/text.

Any help will be deeply appreciated!

CodePudding user response:

Have your .htaccess rules in following manner. This should be able to handle both the cases IMHO. I have changed the rules and regex in your existing rules. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON

#1) redirect file.html to file
RewriteCond %{THE_REQUEST} \s/([^.] )\.html\s [NC]
RewriteRule ^ /%1? [L,R=301]

#2) rewrite /file to file.html
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*)/?$ /$1.html [L]
  • Related