AskApache Logo Redirecting WordPress index.php to root

Search AskApache

4 diggs

Updated: Aug 09, 07 | Redirecting WordPress index.php to root

« Want to know how to really hack?Troubleshooting Apache .htaccess Authentication »

WordPress blogs show the same duplicate content for http://www.askapache.com/index.php and http://www.askapache.com/. 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.

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$ http://www.askapache.com/ [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$ http://www.askapache.com/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.

Reader Comments

Skip to form
  1. Jacob CassJanuary 23, 2008 @ 12:19 am

    Hi,
    I am having a problem that you might easily be able to fix (I can’t as I don’t know much about Apache) because it is related to this post, if you could read my problem at
    http://wordpress.org/support/topic/153081?replies=5#post-681054
    that would be appreciated. Thanks.

  2. MatoNovember 7, 2007 @ 5:30 pm

    Hello im trying and trying something but i cant!

    First i need to say that i have a rule on .htaccess on my root folder with the code:

    Options +FollowSymlinks
    RewriteEngine on
    rewritecond %{http_host} http://www.mysite.com.ar [nc]
    rewriterule ^(.*)$ http://mysite.com.ar/1 [r=301,nc]
    

    I say it because maybe it modify the new thing im trying to do.

    I need a redirect from:
    mysite.com.ar/forum/ -->  mysite.com.ar/forum/index.php

    Is this possible?

    Thanks in advance

  3. AskApacheOctober 4, 2007 @ 10:37 pm

  4. DaveSeptember 14, 2007 @ 12:04 pm

    This code works like a charm. ONE PROBLEM: Now my 404 pages don’t seem to work. If I go to my root and type a page that doesn’t exist, sdfsdfwe.php, the browser acts like that page exists and serves my index instead.

    Is there a way to keep my 404 working?

Comment on "Redirecting WordPress index.php to root"

I review each comment that makes it past my crazy-tight .htaccess anti-spam..
Please wrap code/source with <pre>...</pre> tags, (x)html is allowed and encouraged!

« Want to know how to really hack?Troubleshooting Apache .htaccess Authentication »

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, which lets you use/modify/re-post this content provided you follow the attribution guidelines in the license.