Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess - 301 redirect all files without extension to have .html extension

I need to 301 redirect requests to files with no extension to same with appended .html extension:

http://www.mydomain.com/this

to

http://www.mydomain.com/this.html

The following would not get redirected:

http://www.mydomain.com/that/ (it's a directory)
http://www.mydomain.com/other.php

Any help appreciated on the above, thanks.

like image 841
BrynJ Avatar asked Dec 02 '10 16:12

BrynJ


1 Answers

Try the following. I would place it as the last rule in your set (i.e. the bottom) to not conflict with any other rules.

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

This should ensure the request is not a directory and that is doesn't end with some kind of extension. If those conditions are met, it will append the request with .html.

This is untested, so comment back if it works. ;)

like image 121
Jason McCreary Avatar answered Sep 24 '22 19:09

Jason McCreary