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

Mladen
  • 11
  • 1

3 Answers3

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

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

Community
  • 1
  • 1
Dvir Berebi
  • 1,406
  • 14
  • 25
  • 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 – Mladen Feb 03 '11 at 08:35
  • 1
    Try adding `RewriteBase /` before the rules. [RewriteBase docs](http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase) – Dvir Berebi Feb 03 '11 at 09:45
1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)\.html$ $1.php [L,R=301]
borayeris
  • 2,603
  • 1
  • 27
  • 31
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

]
Himanshu
  • 31,810
  • 31
  • 111
  • 133
Andrew
  • 1