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.

4
  • 2
    is mod_rewrite enabled on your localhost? Commented Aug 7, 2015 at 12:17
  • Not sure. How to check? Commented Aug 7, 2015 at 12:18
  • Your example URL includes /demo/blog/ but your code is expecting /html/ - is this the actual code you are using? Commented Aug 7, 2015 at 12:19
  • @Hardik check this out. stackoverflow.com/questions/9021425/… Commented Aug 7, 2015 at 12:19

4 Answers 4

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]
Sign up to request clarification or add additional context in comments.

1 Comment

this is working only when i enter url without .html.
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

Comments

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.

2 Comments

Thanks for your reply. Already tried this as well. Not working. :(
mod_rewrite is enabled just checked.
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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.