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. 

Tell an amigo:
  • Sphinn
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Facebook



Related posts:
URL Canonicalization (/ vs. index.php) Unfortunately, the concept of "index" pages (index.php, Default.asp, etc.), a...
What Mod_rewrite Won't Do For You I've seen many misconceptions about mod_rewrite.  I believe this is...
SSH2 & PHP Howto Guide: SSH Connections Made Easy in PHP PHP doesn't come with native support for making SSH connections...
SEO With PHP Updates Errata 1. There is a small bug in the cloaking...
Free Chapter From Professional SEO With PHP! Chapter 1 tells you what the book is about, some...