IVCatalina
IVCatalina

Reputation: 356

Removing .html on paths with htaccess

I want to remove the .html off the end of all my links, for example www.mywebsite.com/contact.html should be www.mywebsite.com/contact

The following code does the job:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

I also want my directory files to remove the .html too, i.e. www.mywebsite.com/rates/index.html should be www.mywebsite.com/rates

It is working with the following code:

RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]

But together, they don't work. If the second sits underneath the 1st only the 1st works. If I only have the second option my 1st doesn't work and all my links are broken. How do I combine the two?

Upvotes: 1

Views: 124

Answers (1)

Hitesh Vala Ahir
Hitesh Vala Ahir

Reputation: 793

Add in your .htaccess file

Options +FollowSymlinks -MultiViews
    RewriteEngine on

    # to make `/path/index.html` to /path/
    RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.html [NC]
    RewriteRule . %1 [NE,R=301,L]

    RewriteCond %{THE_REQUEST} ^GET\s.+\.html [NC]
    RewriteRule ^(.+)\.html$ /$1 [NE,R=301,L,NC]

    RewriteCond %{REQUEST_URI} !\.html$ [NC]
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule . %{REQUEST_URI}.html [L]

Upvotes: 1

Related Questions