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. "/index.php?redirect="
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.
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.
- First, login via an ftp client to the directory of your site.
- Next, copy the .htaccess file to your desktop.
- Now, using a text editor (I prefer notepad++), open the file.
- 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]
- 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:
- Login via an ftp client to the folder containing your site.
- Copy the .htaccess file to your desktop.
- Using your text editor, open the file.
- 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]
- 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.
- Login via an ftp client to the folder containing your site.
- Next, copy the .htaccess file to your desktop.
- Then, open the file with a text editor.
- 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]
- 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!