Aug 31

ROTD: Mod_rewrite Rule To Remove Trailing "index.php"

Posted by Jaimie Sirovich on Aug. 31st, 2006. 4 comments — voice your opinion.

NEED A GREAT WEB SITE? NEED IT TO BE SEARCH-ENGINE-FRIENDLY?

SEO Egghead is a web development firm dedicated to creating custom, search engine optimized web site applications. We specialize in eCommerce and content management web sites that not only render information beautifully to the human, but also satisfy the "third browser" — the search engine. To us, search engines are people too. Click here to talk to us. We'd love to help!
X

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:
What Mod_rewrite Won't Do For You I've seen many misconceptions about mod_rewrite.  I believe this is...
URL Canonicalization (/ vs. index.php) Unfortunately, the concept of "index" pages (index.php, Default.asp, etc.), a...
blog.yourcompany.com vs. yourcompany.com/blog It has become popular for companies to create corporate blogs...
SEO-Related Apache Exploit Most deployed versions of Apache are potentially exploitable, as mod_rewrite...
ROTD: Removing CSS Bloat This function, comprised of a simple regular expression, will remove...




"4 Wise Comments Banged Out Somewhere On The Internet ..."


Gemme

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

esoos

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.

http://bonrouge.com/home php

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.

steve

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.



Care To Bang On The Keys ... ?

BECOME AN EGGHEAD. SUBSCRIBE TO OUR RSS FEED!

Learn to be as nerdy as we are by never missing our latest blog entries. Receive great tips, tricks, and ideas on improving your web site every day! Subscribe via our RSS Feed or use the chicklets in the sidebar.