Complete Guide for .htaccess Redirects

Print This Post

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

RSS for Internet Techies Hello there! If you are new here, you might want to subscribe to the RSS feed for updates.
Share it
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…..

Leave a Reply

If you have any query or question related to Blogging, Wordpress, Monetization thru Adsense, Plugins then kindly post your queries on Internet Techies Forum. We will definitely try to resolve that or some other members from our forum will do. Thanks for the consideration.

(required)

(required)