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

Print This Post
RSS for Internet Techies Hello there! If you are new here, you might want to subscribe to the RSS feed for updates.

Featured Jobs On Internet Techies
Digital Media Marketing Specialist at Signature Fencing & Flooring Systems, LLC (New York, New York) : Digital Media Marketing Specialist We are seeking an Digital Media Marketing Specialist who is an expert in the field of SEO/SEM, Social Media marketing and email marketing who can design and impleme...
Hardware Specialist at Confidential (Marlborough, Massachusetts) : For my client, this role is responsible for acting on, closing, and generating sales opportunities from warm leads with little cold calling. Proactively communicates with prospects and existing custom...
One Response to “Complete Guide for .htaccess Redirects”
  1. 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…..

Sorry, the comments are closed on this page. Please use the below link to post us a query.