Page is a not externally linkable
- Code, Content, and Presentation
-- Apache Web Server
---- Rewrite .php extension to virtual folder


g1smd - 5:52 pm on Feb 22, 2010 (gmt 0)


Rules with .* within are usually very inefficient.

This replacement code gets posted several times each week, and fixes those problems:

# Force requests for named index files to drop the index file filename, and force www:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?|php[45]?)(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index\.(html?|php[45]?)$ http://www.example.com/$1 [R=301,L]


--

You need one more redirect after the first listed redirect. It needs to redirect requests for .php URL requests other than those for index.php URLs. It will strip the filename and force the www at the same time.

--

Look again at your first redirect. It is still missing the R=301 flag. Additionally, the domain name needs to be hard-coded into the redirect target. Don't use %{HTTP_HOST} otherwise you will end up with a redirection chain for non-www requests that match both redirects. Here's a post with some code that is 'almost' right, with comments as to why it has problems: [webmasterworld.com...]

What you want is for one (and only one) of the three redirects to match what you need to fix and to fix all problems in one hit. The replacement code above does that.

That is, the first redirect will redirect for any index.php URL request for any folder and for both www and non-www requests. The redirect strips the filename and forces the www domain at the same time.

The next redirect (missing at the moment) fixes .php requests (other than for index.php). It strips the filename, and forces the www at the same time.

The final redirect fixes the www for any non-www URL request not already fixed up by previous redirects.

After the three redirects, there's the rewrite to handle the correct URL requests.


Thread source:: http://www.webmasterworld.com/apache/4084680.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com