Apache - Redirect an Index File to Your Domain Without Looping
The author's views are entirely their own (excluding the unlikely event of hypnosis) and may not always reflect the views of Moz.
The saga of redirecting all requests to https://moz.rankious.com/_moz/index.php to point to https://moz.rankious.com/_moz/ has finally come to an end. We now practice what we preach. For those who are not familiar, it's important to have all duplicate content point to a single, "canonical" edition of the content. Requests to index.php and requests to www.seomoz.org/ have the same content even though they have different URLs. More information on this is available in the beginner's guide to SEO - Canonical Issues and Duplicate Content.
The problem I was having with doing a simple 301 redirect from index.php to the canonical URL is that apache treats both those requests as the same thing. A request to www.seomoz.org is treated as a request to index.php because they're both using the same file. This resulted in apache infinitely redirecting index.php to itself.
In my search for a solution, I found this solution on webmasterworld that apparently works for some, but did not work for me.This is the solution that did end up working:
Note, my solution requires your index file to be using php. It may work for other languages, but they are not documented here.
- Copy index.php to another filename that is not set as a DirectoryIndex by apache, for this example we'll be using sitehome.php
- Create an apache DirectoryIndex directive for your document root. Set it to sitehome.php. Do not set the directive on a server-wide level otherwise it may cause problems with other folders that still need to use index.php as a directory index.
Put this in an .htaccess file in your document root:DirectoryIndex sitehome.php
Or if you aren't using per-directory context files, put this in your httpd.conf
<Directory /your/document/root/examplesite.com/>
DirectoryIndex sitehome.php
</Directory> - Clear out the contents of your original index.php file. Insert this line of code:
<? header("Location: http://www.example.com"); ?>
That should do it. Basically we're just making it so index.php is not a directory index file. This forces sitehome.php to gets read when someone types in the canonical url (https://moz.rankious.com/_moz). Any requests to index.php from old links are redirected while avoiding an infinite loop.
For more information on using 301 redirects, such as for redirect requests to http://example.com to http://www.example.com, read my guide to applying 301 redirects in apache.
Comments
Please keep your comments TAGFEE by following the community etiquette
Comments are closed. Got a burning question? Head to our Q&A section to start a new conversation.