Hardik
Hardik

Reputation: 1283

How to remove .html from url using htaccess

I want to remove .html from url. For this i am trying following. But not sure what is wrong, not working in my case.

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*).html\ HTTP/
RewriteRule .* http://localhost/demo/blog/test.html%1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*)\ HTTP/
RewriteRule .* %1.html [L]

I want to remove .html from this URL http://localhost/demo/blog/test.html. So if someone enter this URL, .html should be removed from URL by htaccess http://localhost/demo/blog/test/.

And need to add this rules only for blog directory.

I hope someone help me.

Upvotes: 0

Views: 421

Answers (4)

Akhil
Akhil

Reputation: 11

First copy paste this code into your .htaccess file.

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

Then change the web addresses on your navigation bar/link like this ↓ From <a href="about.html"> to <a href="about">

It should work, also try not to put capital letters on your file names; It is not a good practice

Upvotes: 1

anubhava
anubhava

Reputation: 784898

You can use these rules in /demo/blog/:

RewriteEngine On
RewriteBase /demo/blog/

RewriteCond %{THE_REQUEST} \s(.+?)\.html?\s [NC]
RewriteRule ^ %1 [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Upvotes: 0

Hitesh Vala Ahir
Hitesh Vala Ahir

Reputation: 793

try this

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

Script47
Script47

Reputation: 14530

That should do it.

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

Edit 1

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

Edit 3

As suggested in the comments, make sure that mod_rewrite is enabled. Use this link to help you out.

Upvotes: 0

Related Questions