This handy set of rules for mod_rewrite automatically redirects any URL ending in "/index.php" to the same URL ending in "/." It works for the root directory as well as subdirectories.
RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [R=301,L]
For example, "http://www.example.com/subdirectory/index.php" would be redirected to "http://www.example.com/subdirectory/."
This eliminates one of the peskier canonicalization issues with relative ease. It's a bit tricky because of the way mod_rewrite updates its variables during execution -- hence the odd use of the variable {THE_REQUEST}. If anyone knows an easier way or finds a problem with it, leave a comment.












September 19th, 2006 at 8:23 am
Thankks. I have been looking for something like this for some time. I'll implement it and in case I run into any trouble, I'll let you know.
Cheers
October 18th, 2006 at 9:20 pm
Nice, I've been using something similar:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.domain.com/%1 [R=301,L]
(hopefully that formats ok.) I think the one you have may be a bit better because it works for subdirectories and php files too.
January 7th, 2007 at 5:16 pm
Hello,
I have a webpage : http://bonrouge.com/br.php?page=home
I want it to be viewed as http://bonrouge.com/home php using the mod rewrite rule. I have tried both of the above codes, but to no avail.
June 5th, 2007 at 2:52 am
This doesnt work for me.
For example, "http://www.example.com/subdirectory/index.php" would be redirected to "http://www.example.com/."
Everything gets redirected to / without the subdirectory. I'm using wamp 1.7.0.