How to Create a Canonical 301 Redirect

301-redirectOur previous article How Canonical URL Redirects (301 Redirects) Can Affect Your Search Rankings provided a brief overview of the effects a lack of canonical (301) redirects can have on your search rankings. Today we would like to tell you how to correct this issue on the major platforms.

A couple of notes to mention regarding the following instructions:

o   For ASP platforms, you will need to create a script to be executed before the content on each page of the site.

  • The Single Page redirects will only transfer one specified page to another individual page.
  • For redirects on Apache servers:

o   Create .htaccess file on root directory of site

o   For sites hosted using Windows, you will need to contact your host provider


PHP Single Page Redirect

(Insert this code within the index.php file)

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.com/page.html");
exit();
?

PHP Canonical Redirect

(Insert this code within the index.php file)

<?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']);
}
?>

Apache .htaccess Singe Page Redirect

(Insert this code in .htaccess file)

Redirect 301 /old/oldpage.htm /new/http://www.domain.com/newpage.htm

Apache .htaccess Canonical Redirect

(Insert this code in .htaccess file)

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

ASP Single Page Redirect

(Insert code before page content)

<%
Response.Status="301 Moved Permanently"
Response.AddHeader='Location','http://www.new-url.com/'
%>

ASP Canonical Redirect

(Insert this code in script)

<%
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
%>

As a side note, if your site was created with WordPress, there are numerous plugins you can choose from to provide additional functionality such as Redirection which allows you to very easily redirect individual pages.

– See more at: https://marketvantage.com/how-to-create-a-canonical-301-redirect/

About The Author

Share This