1

All the URLs on my site use the .html extension. I'd like to make it so that any URLs that are entered into the browser without the .html extension are redirected to the URL with the .html extension.

In summary, I want this

http://example.com/page

to redirect to

http://example.com/page.html

I feel like this question had to have been asked before but I cannot seem to find it.

John R Perry
  • 3,916
  • 2
  • 38
  • 62

1 Answers1

1

You can use this rule in your root .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ /$1.html [L,R=302]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • That almost works. It works for files in the root folder but it redirects all files in subfolders to the root folder. So `http://example.com/subfolder/page` redirects to `http://example.com/page.html` – John R Perry Apr 11 '16 at 19:33
  • That can happen only if your .htaccess is in `subfolder`. – anubhava Apr 11 '16 at 19:37
  • Ohhh, I see. Okay so what if you host multiple sites and don't want all URLs to go to the version with the extension? – John R Perry Apr 11 '16 at 19:38
  • 1
    Nvm I just realized that site's that need the extension removed can have that specified within their folder and it will override the root. Thank you! – John R Perry Apr 11 '16 at 19:42
  • 2
    You should add your answer [over here](http://stackoverflow.com/questions/5745490/rewrite-rule-to-add-html-extension). Your solution worked for me and none of the answers mentioned there solved my problem. – John R Perry Apr 11 '16 at 19:54
  • Thanks, I will do that. – anubhava Apr 11 '16 at 19:56