Skip to content
Search engines 5511dd3

Apache - Redirect an Index File to Your Domain Without Looping

O

The author's views are entirely their own (excluding the unlikely event of hypnosis) and may not always reflect the views of Moz.

Table of Contents

O

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://mza.seotoolninja.com/index.php to point to https://mza.seotoolninja.com/ 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.
  1. Copy index.php to another filename that is not set as a DirectoryIndex by apache, for this example we'll be using sitehome.php
  2. 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>
  3. 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://mza.seotoolninja.com).   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.

Back to Top

With Moz Pro, you have the tools you need to get SEO right — all in one place.

Read Next

Use Google Ads AI & Machine Learning To Run Better Campaigns

Use Google Ads AI & Machine Learning To Run Better Campaigns

Jul 26, 2023
The New Best Practices for Native Advertising on Editorial Sites

The New Best Practices for Native Advertising on Editorial Sites

Nov 17, 2020
Programmatic Delivery: The Future of Content Marketing and Promotion?

Programmatic Delivery: The Future of Content Marketing and Promotion?

Jan 16, 2017

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.