Index Redirect a Web Page

How do I perform an index redirect? Redirecting a web page that uses index.html or index.php files to the root or home page. I recently did some website work for a local business. The business was using a static html website and we wanted to convert the site to a WordPress based platform for multiple reasons.

Index Redirect
Performing an Index Redirect

During the conversion, I needed to not only perform 301 redirects for all of the currently indexed html pages, but also perform a redirect for the index.html page. This was a bit more complicated as you cannot simply perform a 301 redirect on an index page. Here is how I did it.

How to Redirect both index.html and index.php to Home

The following example shows how to perform an index html redirect and index php redirect to your home page.
Remember to replace yoursite.com with your actual site name.

  1. First, login via an ftp client to the directory of your site.
  2. Next, copy the .htaccess file to your desktop.
  3. Now, using a text editor (I prefer notepad++), open the file.
  4. Then, at the top of the .htaccess file add the following code;
    Options +FollowSymLinks
    RewriteCond %{THE_REQUEST} ^.*/index.html
    RewriteRule ^(.*)index.html$ https://www.yoursite.com/$1 [R=301,L]
    RewriteCond %{THE_REQUEST} ^.*/index.php
    RewriteRule ^(.*)index.php$ https://www.yoursite.com/$1 [R=301,L]
  5. Finally, save the changes and upload the .htaccess file to your server, replacing the old file.

Index Redirect index.html to Home Page

To specifically redirect index.html to the home page:

  1. Login via an ftp client to the folder containing your site.
  2. Copy the .htaccess file to your desktop.
  3. Using your text editor, open the file.
  4. Add the following code to the top of the file;
    Options +FollowSymLinks
    RewriteCond %{THE_REQUEST} ^.*/index.html
    RewriteRule ^(.*)index.html$ https://www.yoursite.com/$1 [R=301,L]
  5. Save the changes and upload the .htaccess file to your server.

Index Redirect index.php to Home Page

By default, platforms such as WordPress take care of redirecting index.php to / however, it's not a bad idea to set a rule for this.

  1. Login via an ftp client to the folder containing your site.
  2. Next, copy the .htaccess file to your desktop.
  3. Then, open the file with a text editor.
  4. Add the following code to the top of the file;
    Options +FollowSymLinks
    RewriteCond %{THE_REQUEST} ^.*/index.php
    RewriteRule ^(.*)index.php$ https://www.yoursite.com/$1 [R=301,L]
  5. Finally, save your changes and then upload the .htaccess file to your server, replacing the old file.

From now on, any requests made to index.html or index.php depending on which conditions and rules you used, should result in an index redirect to the home page I.E. yoursite.com

Enjoy!