FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home  »  Htaccess  »  Redirect index.php to root

by 22 comments

WordPress blogs show the same duplicate content for /index.php and /. If you've read about using a robots.txt file for WordPress SEO, than you already understand this setup results in Duplicate Content penalties being levied against your Blog and Web Site by Search Engines.

A common request is to be able to remove or redirect the index.php from appearing in the url. This is possible only with server-side technology like .htaccess configuration files or your main server config by using the Mod_Rewrite Rewrite Module.


The Redirect Fix

The fix is a bit of clever .htaccess code utilizing mod_rewrite to only redirect index.php if the request for /index.php came from a client (e.g. browser or web robot), and not if the request is an internal redirect that apache does when / is requested so that it can serve the contents of index.php.

.htaccess mod_rewrite code

askapache.com/index.php

Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php HTTP/
RewriteRule ^index.php$ / [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

askapache.com/blog/index.php

Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On
RewriteBase /blog/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /blog/index.php HTTP/
RewriteRule ^index.php$ /blog/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

More Info

The problem with this is that any request for "/" is internally rewritten to index.php by Apache mod_dir if you have index.php in the DirectoryIndex list (the usual set-up), so it's difficult to avoid an infinite loop.

Tags

August 9th, 2007

Comments Welcome


Related Articles


My Online Tools
Popular Articles


Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman









[hide]

It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain