- Aug. 31st, 2006
- 4 comments
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.
Related posts:
"4 Wise Comments Banged Out Somewhere On The Internet ..."
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
Nice, I've been using something similar: RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/ (hopefully that formats ok.) I think the one you have may be a bit better because it works for subdirectories and php files too.
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.
This doesnt work for me. Everything gets redirected to / without the subdirectory. I'm using wamp 1.7.0.
|
















