1

I am having a hard time trying to drop the .html for our newly updated website through .htaccess. I have tried a number of different lines of code copy and pasted from the internet, but I'm sure it's something simple that I just don't have the experience to see!

In short, I want to make this request (which is a 404) drop the .html:

http://mysite/cmt-grooving-sblade.html

To this:

http://mysite/cmt-grooving-sblade

Any help you could give will be I will be very grateful for!

So far, my .htaccess looks like this. The first section was already there, the rest is my recent attempt.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Darran
  • 11
  • 2
  • 4
    Possible duplicate of [How to remove .html from URL](http://stackoverflow.com/questions/5730092/how-to-remove-html-from-url) – ppasler Jan 25 '17 at 14:24

2 Answers2

1

You can use this to remove .html from your URLs:

RewriteEngine on

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

It will leave you with your desired URL: http://mysite/cmt-grooving-sblade. Make sure you clear your cache before testing this.

Joe
  • 4,877
  • 5
  • 30
  • 51
  • Thank you very much for the help, but unfortunately this doesn't seem to work. I suspect I might be putting it in the wrong place. In my .htaccess, I have this pasted at the bottom, just below the other code (that I am hesitant to touch as it was there before I started playing around with it!) I tried it with and without the rewrite engine as I see it is started with the code above it, and I have the at the very end. You're going to hit me with a hammer, as I'm sure it's my error! – Darran Jan 25 '17 at 23:18
  • I should maybe clarify. The website at the moment doesn't show .html on any urls but the old one did. I have 404s coming from Google that are looking for the .html links, what I would like to do is direct all .html requests to drop the .html, which will land on the correct page. Many thanks again for the help so far. – Darran Jan 26 '17 at 13:37
0

Try this;

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
Joe
  • 4,877
  • 5
  • 30
  • 51
KOUSIK MANDAL
  • 2,002
  • 1
  • 21
  • 46
  • Thank you for the answer. Will this remove the .html? In my .htaccess the first part of the code works (as far as I'm aware), but it's the .html that has me pickled. – Darran Jan 25 '17 at 23:19