Smartlab Software logo

Redirect an Index page to /

Depending on the type of home page you have (index.html or index.htm or index.php) it should be redirected to the directory only. This keeps the search engines happy as they only deal with one way to get to your home page (or default page for any directory). This method only works for an Apache server.

index.html vs. index.php vs. /

www.mydomain.com/index.html and www.mydomain.com/ are not the same URL from a search engine perspective. If someone adds the index.html while typing in your domain they should be redirected to your folder name which ends in a slash (www.mydomain.com/) Once again, htaccess comes to the rescue.

Using a RewriteRule will alleviate the problem. The solution below will take effect for ALL subdirectories. To redirect www.mydomain.com/index.html to www.mydomain.com/ do the following:

In an existing .htaccess file or a new .htaccess file add the following code:

Options +FollowSymLinks
RewriteEngine On
#
# redirect index.htm and index.html to / (do this before non-www to www)
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ http://www.mydomain.com/$1 [R=301,L]

Replace mydomain with the domain you want to redirect. r=301 means redirect the client and send a status code of 301. This will redirect index.html AND index.htm to the root directory /.

 For example, if you typed in www.mydomain.com/products/index.htm you will be redirected to www.mydomain.com/products/ which is what we want.

If you also want index.php redirected:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.(html|php)?\ HTTP/
RewriteRule ^(.*)index\.(html|php)?$ http://www.mydomain.com/$1 [R=301,L]

Since the RewriteCond and RewriteRule use pattern-matching, index.ph will also be matched (but I wouldn't worry about it).

Redirect Solution

The following htaccess code will take care of www vs. non-www and redirect index.html or index.htm to /. Note the index.html redirection to www.mydomain.com/ is done before non-www to www redirection.

Options +FollowSymLinks
RewriteEngine On
#
# redirect index.htm and index.html to / (do this before non-www to www)
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ http://www.mydomain.com/$1 [R=301,L]
#
# redirect mydomain.com -> www.mydomain.com
#
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

Test it Out

Test out the following scenarios. They should all end up at http://www.mydomain.com/

  • mydomain.com
  • mydomain.com/
  • mydomain.com/index.html
  • mydomain.com/index.htm
  • mydomain.com/index.php
  • www.mydomain.com
  • www.mydomain.com/
  • www.mydomain.com/index.htm
  • www.mydomain.com/index.html
  • www.mydomain.com/index.php

If you have any subdirectories that have a default index page, you're covered.

If you view the response code (I use the free http debugger Fiddler - no I don't use it on the roof. Fiddler works out-of-the-box with IE and Google's chrome but not Firefox which takes some configuration) it will be 301 redirect except for www.mydomain.com/ which will be a 200 or 304 status code. Click here for a handy list of the common http status codes.