1

First to say, sorry if I'm doubleposting, but I didn't find a solution to this one.

I'm trying to rewrite my urls so that, whene I enter somepage.html it serves me somepage.php but ONLY if somepage.html doesn't exist. Anyone can help? Thanks

3 Answers 3

3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)\.html$ $1.php [L,R=301]

also see Redirect requests only if the file is not found?

Sign up to request clarification or add additional context in comments.

2 Comments

sorry, but this isn't working for me... for example: www.myurl.com/something.html works fine if the file is there, but if there is no file, it tries to grab (and changes url to): www.myurl.com/emformar/domains/myurl.com/public_html/something.php
Try adding RewriteBase / before the rules. RewriteBase docs
1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)\.html$ $1.php [L,R=301]

Comments

0

I had the same issue, this was my solution.

This will check to see if there is a HTML file present, if not it will use PHP - ideal if you have migrated your website to PHP and are concerned about any 3rd party links to HTML pages

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.html$ $1.php [R=301,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.