Mobile app version of vmapp.org
Login or Join
Ogunnowo487

: .htacess - Remove .html from the homepage but not from folders within within that website I'm trying to remove the .html from my landing page, an example would be instead of domain.com/contact.html

@Ogunnowo487

Posted in: #Apache #Htaccess #Html #Redirects

I'm trying to remove the .html from my landing page, an example would be instead of domain.com/contact.html it would be domain.com/contact while at the same time keeping the .html for all the folders within the website, so domain.com/components/file.html would stay as a .html..

So in other words redirect for the landing page, but not for all the other files on my server..

This is the code I'm currently using to remove the .html but it also removes it for all the other directories as well.
#example .com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ .html [L,QSA]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*.html HTTP/
RewriteRule ^(.*).html$ / [R=301,L]


Would anyone have any advice on this matter?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Ogunnowo487

2 Comments

Sorted by latest first Latest Oldest Best

 

@Ravi8258870

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*.html HTTP/
RewriteRule ^(.*).html$ / [R=301,L]



Let's focus on this one, as the one you want to change. Firstly I would say the THE_REQUEST is just adding complication. I guess its purpose is to 'stop' the rule adding the html back, removed during the first 'internal' redirect.

... I would suggest that it's much better just to 'fix' that by putting the URL first. ie. have the 301 removing the extension FIRST. (It has L in it anyway.) Then have the internal redirect.

Then it seems you just need

RewriteRule ^([^/]+).html$ / [R=301,L]


[^/] means match anything except a slash. So then paths with a slash in them (eg folders!) won't match.

You may also want to consider adding:

RewriteBase /


to the beginning of the file to make sure that relative paths work. ie that the rewrite rule isn't matching a path that already has slashes at start.

Alternatively use:

RewriteRule ^/?([^/]+).html$ / [R=301,L]

10% popularity Vote Up Vote Down


 

@Nimeshi995

Never mind, I found the solution:

DirectorySlash Off
RewriteEngine On

RewriteCond %{THE_REQUEST} s/+(.*?/)?(?:index)?(.*?).html[s?/] [NC]
RewriteRule ^(.[^/]*)$ /%1%2 [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*) /.html [L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme