0

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.

2
  • Sure, sounds like a good plan. Go for it! Or did you have a question here? Commented Jan 8, 2019 at 7:26
  • Updated question. Commented Jan 8, 2019 at 7:28

1 Answer 1

1

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).

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

3 Comments

Thank you works like a charm! [L] works well in my environment. Btw, I tried defining those script_filenames just once above first RewriteRule and still working fine. Will I miss something if I define it once?
The code does not "define script_filenames" at all... Those are conditions. Do as you like, will work likewise in most situations, sure. About the [L] flag: you should definitely prefer the [END] flag if your http server supports it even if the [L] flag works. That could prevent interactions between different rules if you add more complex rewriting code.
No site, but simply the official documentation of apache's modules. They are, as typical for OpenSource software, of excellent quality and come with great examples. Start here: httpd.apache.org/docs/current/mod/mod_rewrite.html & httpd.apache.org/docs/2.4/howto/htaccess.html

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.