How to do 301 redirects in Apache htaccess

How to do 301 redirects in Apache htaccess, IIS, PHP, ASP and ColdFusion

Establish proper search-friendly 301 redirects to manage duplicate content issues and changes to your site’s information architecture (URL structure) with the specific instructions for your server software below:

Apache Web Server .htaccess file
Apache .htaccess files let you override the Web server configuration on a per-directory hierarchial basis, and are instrumental in the creation of proper 301 redirects. For more information, consult the _Apache .htaccess Tutorial _ and the _Apache URL Rewriting Guide _.

Single Page Redirect:
Redirect 301 /old/url /new/url
or
Redirect Permanent /old/ur /new/url

Canonical Hostname Redirect (non-www to www):
This solution will redirect any page requested via a non-www domain to the same URL with the www domain, and as such it can be placed in the Document Root of your site and will be enforced globally:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

Microsoft IIS Web Server

Note: these instructions require administrative access to IIS. If you do not have this access (e.g., if you have a shared hosting account on a Windows server), you should use one of the server-side scripting methods such as ASP or PHP given further below.

Single Page Redirect:

  1. Open Internet Services Manager and right-click on the file or folder you wish to redirect.
  2. Select the radio button “a redirection to a URL”.
  3. Enter the desitnation page for the redirect.
  4. Check “The exact url entered above” and the “A permanent redirection for this resource”.
  5. Hit “Apply”.

 

ColdFusion

Single Page Redirect:

Active Server Pages (ASP)

Single Page Redirect:
<% Response.Status=”301 Moved Permanently” Response.AddHeader=’Location’,’http://www.new-url.com/’ %>

Canonical Hostname Redirect (non-www to www):
This code should be inserted into a global include file or any ASP script which is executed for every page on the site before the page output begins:
<% If InStr(Request.ServerVariables(“SERVER_NAME”),”www”) = 0 Then Response.Status=”301 Moved Permanently” Response.AddHeader “Location”,”http://www.” & Request.ServerVariables(“HTTP_HOST”) & Request.ServerVariables(“REQUEST_URI”) Response.End End if %>

PHP 4 and PHP 5

Single Page Redirect:
header(‘HTTP/1.1 301 Moved Permanently’);
header(‘Location: http://www.new-url.com/’); ?>

Canonical Hostname Redirect (non-www to www):
<% If InStr(Request.ServerVariables(“SERVER_NAME”),”www”) = 0 Then Response.Status=”301 Moved Permanently” Response.AddHeader “Location”,”http://www.” & Request.ServerVariables(“HTTP_HOST”) & Request.ServerVariables(“SCRIPT_NAME”) Response.End End if %>

Still Facing difficulties: Contact SEO India Company, Profit By Search.

Get a Free Audit