SEO Image Blog

301 Redirects & Canonical URL Tutorial

301 redirects tutorial
by Alan Rabinowitz

Test Your Redirects with Our Redirect Checker Tool:

How to use canonical and server-side redirects for SEO

When you move a file on your server you want to be certain that you pass the proper redirect directive to the browsers and more importantly, the Search Engines. The 301 redirect is the only redirect we recommend for anyone to use without advanced knowledge of using redirects. 302 redirects are actually used by SEO’s for some specific reasons such as moving domain names from an old domain.com to a new domain.com

Apache | ASP | ASP.NET | Windows IIS | Coldfusion | Sun

301 Redirects & Canonical Redirects for Apache

This is done on the invisible file named “.htaccess” this file is located in the root of your Web Server.

See how to Force all Pages to https below…

For a single page redirect (NEVER use a 302 make sure it says either “Permanent” or “301” in the file if it doesn’t it will pass a 302 by default:

redirect 301 /old-file-name-html http://www.yourdomain.com/new-file.html

or

redirect permanent /old-file.html http://www.yourdomain.com/new-file.html

Notice all that is needed is one space between the old path and the new URL. The new URL must contain an Absolute URL: meaning it uses http://www.yourdomain.com/yourfile.html. If there is a space in the file name use %20 otherwise it will not work, you can only leave one space and that is between the old file path and the new url. Here is an example:

/old%20file.html http://www.yourdomain.com/new-file.html

Apache Server Canonical Redirect
(non-www to www):

This should always be done as some search engines do not properly understand the difference and may count it as a duplicate page, additionally if you use domain.com/index.html or domain.com/index.php the Search Engine might get lost. Since Google is the most popular Search Engine, it also happens to be a bit archaic in its handling of canonical issues and redirects. What Google will see as the same page duplicated on your site (In reality it’s all the same page).

domain.com
domain.com/
www.domain.com/
www.domain.com
domain.com/index.html
www.domain.com/index.html

Since Google favors penalties over relative content you need to make sure you NEVER link to your home page as “index.html” or any other file extension. The automated factors of the Google algorithm would hurt your rankings without warning assuming that you have a higher potential of spam from duplicate content. Google generally does not notify webmasters of penalties, so you could be spending years trying to resolve an issue like this. Make all your links Absolute (Full URL) at least to your home page, so any link on your site should always link to “http://www.yourdomain.com/” be sure to use the trailing slash as some servers will pass a redirect to the trailing slash if you do not, and this will be a 302 redirect. This is a known issue on WebStar Server, an old Mac Platform web server that has no redirect capabilities.

ALWAYS:
http://www.yourdomain.com/
NEVER:
http://www.yourdomain.com/index.html -Not direct to the domain.
http://www.yourdomain.com – No Trailing Slash

Now for the .htaccess canonical redirect for the NON-www. to the www.

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

NOTE: Some Versions of Apace require an extra line or two of code for SymLinks and a different structure of the rewrite rule as follows:

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

To Move To a NEW Domain and redirect both www and non-www versions:
(This is a change of address for a domain. Old Domain to New Domain 301 Redirect)

RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain\.com [NC]
RewriteRule ^(.*)$ http://www.NEW-domain.com/$1 [L,R=301,NC]

A More advanced version will even allow you to 301 your IP address and any subdomain:

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

The above code will 301 your non-www to your www, as well as redirect your IP address as a 301 to your www.

For IP Redirection Use:

RewriteCond %{HTTP_HOST} ^12\.34\.567\.890
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

For www to non-www:

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

For Hosting on Certain Servers:

Godaddy .htaccess use:

RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

When using the www. for Godaddy use:
RewriteEngine on
rewritecond %{http_host} ^www\.domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

In the above we removed the ! that we use on other hosting systems, also note the use of capital and lowercase where it would appear as: RewriteCond %{http_host} !^www\.domain.com [nc]

To redirect your http pages but NOT your https pages use:
Say for instance, your secure certificate was not created as www.domain.com, but was instead issued as domain.com then you would need to use the following and be sure to combine this with ABSOLUTE links (absolute links use the full URL), or you may unintentionally cause duplicate https and http paths.

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

To Redirect ALL HTTP pages to HTTPS using a 301: (Force all pages to https)

RewriteEngine On
RewriteCond %
{HTTPS} off
RewriteRule ^
(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Additionally some servers require an extra line of code:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

For Redirection of a directory to another directory on the same site:
RedirectMatch 301 ^/old-folder/(.*) http://www.yourdomain.com/newfolder/$1

You can even use a lower-level directory and redirect to a top-level directory:
RedirectMatch 301 ^/old-folder/directory(.*) http://www.yourdomain.com/newfolder/$1

If your pages are dynamic you can use:
RedirectMatch 301 ^/old-folder/?(.*) http://www.yourdomain.com/newfolder/$1

To redirect all pages of a directory you no longer need/use (removing /blog/) to the root:
RewriteRule ^foldername/(.*)$ /$1 [R=301,NC,L]

301 Redirects & Canonical Redirects:
Microsoft IIS Web Servers

To Perform this you need administrative access to the IIS Server Application. If you do not have this then you can do the redirects in ASP or PHP.

Single Page Redirect:

  1. Open Internet Services Manager then “right-click” on the file or folder you want to redirect to a new page or folder.
  2. Select “redirection to a URL”.
  3. Enter the URL where you want the redirect to go to.
  4. Check the fields: “exact URL entered above” and “A permanent redirection for this resource”.
  5. Click “Apply”.

PHP 301 Redirect
This is added to the page that you want to redirect to a new page, it is triggered on the request for the specific page and the file will actually return the redirect.

Single Page Redirect:

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

PHP Canonical Redirect:
(non-www to www)

<?php
if (substr($_SERVER[‘HTTP_HOST’],0,3) != ‘www’) {
header(‘HTTP/1.1 301 Moved Permanently’);
header(‘Location: http://www.’.$_SERVER[‘HTTP_HOST’]
.$_SERVER[‘REQUEST_URI’]);
}
?>

Redirects & Canonical Redirects for Active Server Pages (ASP)

Single Page Redirect:
This is placed on the page you are redirecting and is triggered when someone tries to access the page.

<%@ Language=VBScript %>
<%
Response.Status=”301 moved permanently”
Response.AddHeader “Location”, “http://www.domain.com/”
%>

ASP Canonical Redirect:
(non-www to www):
This code should be inserted into a global include file or any script which is executed for every page on the site before the page output begins. If your programmers are using ASP.net and/or they are not using a global file, you need to have them address this issue or perform the Canonical Redirect from the IIS Server Application (as above).

<%
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”)
End if
%>

301 Redirects for ASP.NET

For Single Pages:

<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(“Location”,”http://www.domainname.com/new-page.aspx”);
}<…

Canonical Redirect for ASP.NET (ASPX):

For ASP.NET you need to do this individually in the Windows IIS Internet Services Manager or Use The ASP Code above in a global file that will process the redirect BEFORE any other processes.

 

301 Redirects for Macromedia ColdFusion

Since cold fusion is usually hosted on Windows, Linux, and Sun. The .htaccess and Windows IIS tutorials above will have solutions for the Canonical and redirects. This is for a Single Page Redirect embedded directly into the file:

<cfheader statuscode=”301″ statustext=”Moved permanently”>
<cfheader name=”Location” value=”http://www.new-location.com/”>

301 Redirects for Sun Web Servers

In order to do this on a SUN platform you have to rewrite the SUN Web Server’s standard action of always using a 302 redirect to always use a 301 redirect. You have to open the obj.conf configuration file and add the following lines below the <Object name=”default”> line:

<Client code=”302″>
Output fn=”set-variable” error=”301″ noaction=”true”
</Client>

The above will make the SUN webserver to return 301 permanent redirect responses for all redirects. If you only want to use a 301 permanent redirect response for certain URLs or files, you can add the following to the bottom of obj.conf:

<Object ppath=”path”>
<Client code=”302″>
Output fn=”set-variable” error=”301″ noaction=”true”
</Client>
</Object>

 

Top | Apache | ASP | ASP.NET | Windows IIS | Coldfusion | Sun



BEFORE YOU GO... WE GET RESULTS!

    SEO & Reputation Management Services in NYC & Long Island

    We'll Get You Rankings & Improve Your Reputation
    Get Started - 888-736-2667

    X