0

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.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Hardik
  • 1,283
  • 14
  • 34

4 Answers4

1

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]
Hitesh Vala Ahir
  • 773
  • 2
  • 13
  • 27
1

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

Akhil
  • 11
  • 1
0

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.

Community
  • 1
  • 1
Script47
  • 14,230
  • 4
  • 45
  • 66
0

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643