The saga of redirecting all requests to https://moz.com/index.php to point to https://moz.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.
- 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: https://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.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 https://example.com to https://www.example.com, read my guide to applying 301 redirects in apache.
I just faced this problem, Moz was the first in mind to ask question. my site is showing www.xyz.com/index-html/
i tried to redirect it but was not useful. I was reading some articles on this, But my answer was written here . :))
Oatmeal, I tried your method but now google also lists the file specified by DirectoryIndex... So now I have https://www.myurl.com/ and https://www.myurl.com/home.php showing up as duplicate pages in google
Did you fix the problem?
I have the same problem.
Please let me know.
That's kinda like using duct tape to fix things...
Try adding this to your virtualhost in apache
ServerName seomoz.org
ServerAlias seomoz.org
ServerAlias *.seomoz.org
RewriteEngine on
RewriteRule ^/index.php$ https://www.seomoz.org/
That's definitely the better approach. Much cleaner.
1. As the others have pointed out, doesn't this just end up creating a new page that will be seen as a duplicate?
2.We use IIS, so anyone have any ideas on how handle this with IIS?