2

I have a rewrite condition and rule which is.

RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^[^*]+$ index.php [L]

I need to replace .html with .php in %{DOCUMENT_ROOT}$0.

The reason is, I am rewriting my url's to .html but when this file checks for an existing file it fails due to %{DOCUMENT_ROOT}$0 looking for file thefile.html,

I need it to look for thefile.php.

kba
  • 19,333
  • 5
  • 62
  • 89
Ian Wilkinson
  • 141
  • 1
  • 1
  • 13
  • Keep in mind this WILL NOT interpret HTML as PHP. Which seems like the only real reason one would think this. – Cole Tobin Jun 09 '12 at 23:55

2 Answers2

9

For rewriting .html extensions to existing .php files with the same name, try this rule instead of what you had:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php?%{QUERY_STRING} [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L]

What you have just rewrites any non-existent file to index.php

drew010
  • 68,777
  • 11
  • 134
  • 162
  • Thanks, however, if the file does not exist I get a 404 error with that code. I need it to goto the file if it exists as .php (but the request will be .html), if not got index.php – Ian Wilkinson Jun 09 '12 at 23:55
  • I added 2 more lines to the end so it should in addition, redirect non-existent files to index.php – drew010 Jun 10 '12 at 00:00
  • For those looking for an explanation, the second RewriteRule works because .htaccess is called on the second pass when looking for the new PHP file. – Scott Stevens Jun 10 '12 at 01:17
4

Solved!

RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^(.*).html$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^[^*]+$ index.php [L]

I had to use it twice to firstly, rewite all .html to .php then on the request for.php the second condition comes into play.

Ian Wilkinson
  • 141
  • 1
  • 1
  • 13