index.php to root redirects

As well as non www to www redirects, there is a need for redirects from index.php or index.html to the root of a domain.

See the redirect check for a customised recommendation for your website.

The general rule

In SEO, we want one url for each page of content. Redirects from non www to the www version of the url, from index.php to /, using only /content/ rather than /content/pg1 in a multi page document... There are many possibilities for multiple url's for one page of content. All need to be corrected via redirects as well as fixing the underlying code, and using Google's pagination rel=next/prev or canonical url's.

index.php to root

Personally I try and change the code on the pages so that the links go through directly to the proper place. However this may be too hard work, but more importantly, there many be links to the full url. Google may have cached a copy of the url, and so be revisiting it. So there needs to be redirects from index.php to the proper url to preserve Google PR.

Google is intelligent enough to pick some of the obvious double-ups, but it is better that as a programmer you make your site clean.

You need to be very careful using this code, as for instance the admin side of the site or some ajax function may be using /index.php?bla=value and the below code may need some exceptions added to it... The Concrete5 CMS is one such example...

either: to or to


The Options +FollowSymLinks is optional and may need to be removed depending on your hosting setup.

Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$$1 [R=301,L]

If you add the following to the first php of every index.php file, it will 301 redirect to the root. You can create a function/class or an include file that includes the following code:


if (PHP_VERSION >= '4.3.0')
header("Location: $url", 0, 301);
else if ($sapi == 'cgi' OR $sapi == 'cgi-fcgi')
header("Location: $url");
// Call the status header after Location so we are sure to wipe out the 302 header sent by PHP
header('Status: 301 Moved Permanently');
header("Location: $url");
header('HTTP/1.1 301 Moved Permanently');

Please test that it works, no guarantees for your particular server configuration.

The following script will give the values of all your server variables on your system:

<? print_r($_SERVER);?>

(copying some old posts from an old forum of mine that I wish to keep)