How to Do 301 Redirect in htaccess

WordPress, Joomla, Magento, ZenCart, OsCommerce, VBulletin etc are most commonly used PHP applications. If your website is based on any of these opensource scripts or else it’s a custom PHP application, you need Apache Server to run the scripts. Apache Servers normally work with Linux hosting, but can be installed on Windows hosting too.

This tutorial will guide you on redirecting following URLs to http://www.yoursite.com using htaccess on Apache servers.

  • http://yoursite.com
  • http://yoursite.com/index.php
  • http://www.yoursite.com/index.php
  • http://yoursite.com/index.htm
  • http://www.yoursite.com/index.htm
  • http://yoursite.com/index.html
  • http://www.yoursite.com/index.html
  • http://yoursite.com/index.shtml
  • http://www.yoursite.com/index.shtml

Instructions

  • 1

    Make sure that your hosting service has provided you with Apache Server in case you’re on Windows hosting, else skip this step.

  • 2

    Confirm that Apache Rewrite Module is turned on. On Linux servers, it’s turned on by default, but in case if it’s not, you’ll need to ask the hosting company. If you’re an advance user and have access to the httpd.conf file, you can turn the module on yourself. To do so, first take backup of httpd.conf file and then uncomment the following line:

    LoadModule rewrite_module modules/mod_rewrite.so

    Save the file and restart the server.

  • 3

    Login to your FTP and Download .htaccess file which is located at the root web folder.

  • 4

    Save a copy of .htaccess file somewhere else for backup purpose and then open the current file for editing. It’ll open in notepad.

  • 5

    Add the following code to .htaccess file. Replace “yoursite.com” with your own website and do not change anything else.

    RewriteEngine On
    RewriteBase /

    Redirect 301 www.yourdomain.com/index.html www.yourdomain.com

    Redirect 301 www.yourdomain.com/index.shtml www.yourdomain.com

    Redirect 301 www.yourdomain.com/index.shtml www.yourdomain.com

    RewriteCond %{HTTP_HOST} ^yoursite.com
    RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]

  • 6

    Now save the file.

  • 7

    Upload edited file back to the location from where it was downloaded.

  • 8

    Once uploaded, type each URL to verify if it’s working or not. If working, it should redirect immediately.

  • 9

    To cross verify, visit http://www.ragepank.com/redirect-check/ and enter your URL. It should display all the results as 301 moved to the www version of your domain else at the end of processing it gives you additional feedback too.

  • 10

    If it’s still not working, copy the backed up .htaccess file and start from the beginning and see you might have missed some instructions.

Leave a Reply

Your email address will not be published. Required fields are marked *


one + 3 =