Skip to content Skip to sidebar Skip to footer

How To Remove .html From Url?

How to remove .html from the URL of a static page? Also, I need to redirect any url with .html to the one without it. (i.e. www.example.com/page.html to www.example.com/page ).

Solution 1:

I think some explanation of Jon's answer would be constructive. The following:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

checks that if the specified file or directory respectively doesn't exist, then the rewrite rule proceeds:

RewriteRule ^(.*)\.html$ /$1 [L,R=301]

But what does that mean? It uses regex (regular expressions). Here is a little something I made earlier... enter image description here

I think that's correct.

NOTE: When testing your .htaccessdo not use 301 redirects. Use 302 until finished testing, as the browser will cache 301s. See https://stackoverflow.com/a/9204355/3217306

Update: I was slightly mistaken, . matches all characters except newlines, so includes whitespace. Also, here is a helpful regex cheat sheet

Sources:

http://community.sitepoint.com/t/what-does-this-mean-rewritecond-request-filename-f-d/2034/2

https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules

Solution 2:

To remove the .html extension from your urls, you can use the following code in root/htaccess :

RewriteEngine on


RewriteCond %{THE_REQUEST} /([^.]+)\.html[NC]
RewriteRule ^ /%1[NC,L,R]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html[NC,L]

NOTE: If you want to remove any other extension, for example to remove the .php extension, just replace the html everywhere with php in the code above.

Also see this How to remove .html and .php from URLs using htaccess` .

Solution 3:

This should work for you:

#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 ^(.+)$ $1.html [L,QSA]

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

Solution 4:

With .htaccess under apache you can do the redirect like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 

As for removing of .html from the url, simply link to the page without .html

<ahref="http://www.example.com/page">page</a>

Solution 5:

You will need to make sure you have Options -MultiViews as well.

None of the above worked for me on a standard cPanel host.

This worked:

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

Post a Comment for "How To Remove .html From Url?"