Ian Wilkinson
Ian Wilkinson

Reputation: 141

Rewrite .html to .php using mod_rewrite

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.

Upvotes: 2

Views: 12498

Answers (2)

Ian Wilkinson
Ian Wilkinson

Reputation: 141

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.

Upvotes: 4

drew010
drew010

Reputation: 69977

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

Upvotes: 9

Related Questions