Complete Guide for .htaccess Redirects

We face different scenarios where we need to redirect some specific sites to certain locations. There are two kind of redirects – Permanent (301) or Temporary (302) redirects. You can achieve these redirects based on your scenario by changing .htaccess file available in your root server. Here are collection of .htaccess redirects for different scenarios.

 redirects-htaccess

Redirect complete site to any other domain

#Redirect complete site to any other domain

Redirect 301 / http://www.example.com/

     

Redirect .html extensions to .php extensions

RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php

 

Redirect index.html to specific folder under root

# This allows you to redirect index.html to a specific subfolder

Redirect /index.html http://example.com/newdirectory/

 

Redirect visitors to WWW

RewriteEngine On RewriteCond %{HTTP_HOST} !^www\example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

 

Redirect visitors to non WWW

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

 

Redirect Single Page to some other location

Redirect 301 /oldpage.html http://www.example.com/newpage.html

 

Put https:// for every page on your site (Secured Version)

RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

 

Put https:// for pages in particular folder

RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} somefolder RewriteRule ^(.*)$ https://www.example.com/somefolder/$1 [R,L]

Related: 9 Rules to have Search Engine Friendly Slug or Permalink for Posts

Sanjeev Mishra is a professional blogger and an Internet Marketing Consultant based in India. He has built the Internet Techies to provide you updates in technology and web application area.

1 Comment

  1. Debajyoti Das

    November 25, 2009 at 11:03 pm

    Excellent Guide …. Thanks…

    Any Method for Redirecting Old Permalinks to New One … Like In
    /2009/09/postname.html to /postname/ using htaccess ?

    I am currently using “Redirection Plugin by John Godley”. Its a fantastic redirection plugin with various customizations. But it puts a bit of load on the servers…..

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>