SomethingOn
SomethingOn

Reputation: 10891

htaccess rewrite to add .html if it exists otherwise leave the URL as is

I have a situation where I need to do a URL rewrite IF a file exists otherwise leave the URL as is.

Here's an example:

http://mydomain.com/section -> if http://mydomain.com/section exists go to -> http://mydomain.com/section.html
http://mydomain.com/section -> if http://mydomain.com/section.html DOES NOT exist go to -> http://mydomain.com/section

How can I do this with a rewritecond?

Thanks!!

Upvotes: 1

Views: 225

Answers (1)

anubhava
anubhava

Reputation: 785266

You can try this rule in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ /$1.html [L,R=302]

Upvotes: 1

Related Questions