Jeaf Gilbert
Jeaf Gilbert

Reputation: 11981

Htaccess Pretty URL PHP HTML

I only want to redirect

http://thehost/pagename to http://thehost/pagename.php

and http://thehost/pagename.html to http://thehost/pagename.php.

It is also must support: http://thehost or http://thehost/ (to index.php)

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^/(.*)\.html $1.php [L]
RewriteRule ^/([^/]*)$ /$1.php [L]

Lost. Help.

Upvotes: 0

Views: 22

Answers (1)

arkascha
arkascha

Reputation: 42885

I'd say this should be what you are looking for:

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^/?([^/]*)$ /$1.php [END]

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/?(.*)\.html$ /$1.php [END]

If you receive an "internal server error" with these lines (http status 500) then chances are that you operate an extremely old version of the apache http server and should either upgrade or replace the [END] flag with the old [L] flag. You will see clear hints on that in your http servers error log file in that case.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Upvotes: 1

Related Questions