Articles SEO

How to Use Redirect 301 Correctly on Various Server Platforms

4.67/5 (3)

Redirect 301 or the 301 Permanent Redirect is used in several cases: when we need to change the domain name, move the web page or use site URL with/without “www”. 301 redirect is highly important for the Page Rank (PR) and search traffic.

Redirect 301 is a very popular theme on most SEO forums. Each search engine provides its own tips on how to use this function correctly. In this blog post we’ve united most popular and efficient ways of using Redirect 301.

301 redirect is the best way to preserve your PR in search engines if you are moving your website or a single web page. 301 code is interpreted as the page is being “moved permanently”.

301 Redirect Tips, Tricks and Info

  • 301 redirects are instant, because the .htaccess file is read before the page loads;
  • no need to set up a 301 redirect to pages that have no backlinks, unless they have good PR;
  • when you've done a 301 redirect, check all pages involved: if you've redirected a main page, check both that page and several of its sub-pages to make sure they are behaving the way you wanted them to;
  • 301 redirects pass all page rank and SERPs (Search Engine Results Pages rankings) - making them a vital part of a SEO roadmap for a changing site.

Benefits of the Redirect

A web page may be redirected for several reasons:

  • A web site might need to change its domain name;
  • An author might move his or her pages to a new domain;
  • Two web sites might merge.

With URL redirects, incoming links to an outdated URL can be sent to the correct location. These links might be from other sites that have not realized that there is a change or from bookmarks/favorites that users have saved in their browsers.

***

1. Simple Redirect in .htaccess or httpd.conf for Apache

1
Redirect 301 / http://www.you.com/new.htm

Here Redirect 301 is an instruction saying that the page is moved, / - Means that all top-level sites including all sub-directories, will be forwarded, http://www.you.com/new.htm is a new page or website (do not forget to put the last / if forwarding is on the site).

To save the PR when redirecting the page use the following command:

1
Redirect 301 /old/old.htm http://www.you.com/new.htm

Here /old/old.htm is a address and name of an old page.

Similar syntax is used to redirect the website:

1
RedirectPermanent / http://www.you.com/

Example of catalogue forwarding:

1
RedirectPermanent /old-directory http://www.domain.com/new-directory/

For example, users who come entered the test will be redirected to www.test.com, others to enter.test.com (the order of the records is highly important):

1
2
Redirect permanent /test    http://www.test.com/
Redirect permanent /        http://enter.test.com/

***

2. Using mod_rewrite is written through the .htacess file

The classical problem of using site URL with or without www can be solved in the following way:

1
2
3
4
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite\.com
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=permanent,L] .

Or an alternative syntax:

1
2
3
4
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
[R=301,L] L means the last rewrite rule and stops looping and/or conflicts.

Redirecting form an old domain name to the new one:

1
2
3
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

For example is you need to load rewrite.html instead of rewrite.htm add to the .htacess file following command:

1
2
3
RewriteEngine   on
RewriteBase     /
RewriteRule     ^rewrite\.htm$  rewrite.html [R=permanent]

To replace all .htm files with .html ones:

1
2
3
RewriteEngine  on
RewriteBase     /
RewriteRule     ^(.*)\.htm$  $1.html [R=permanent]

***

3. How to make redirect using PHP:

1
2
3
4
5
<!--?php 
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.ru/newdir/newpage.htm");
exit();
?-->

It will be better to specify HTTP/1.1, as the older versions do not support virtual hosting. Do not forget that before addressing the header, any command should be displayed (for example, echo or print). Therefore, this code is better to be put the top of the php-script. A more complete version of php redirect with preservation of the transmitted page and call options:

1
2
3
4
5
6
7
&lt;?
$ref=$_SERVER['QUERY_STRING'];
if ($ref!='') $ref='?'.$ref;
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://newdomain.com/'.$ref);
exit();
?&gt;

***

4. Using ASP redirect:

1
2
3
4
5
6
&lt;%@ Language=VBScript %&gt;
&lt;% 
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "http://www.new-url.com"
response.end
%&gt;

***

5. Using ASP.NET redirect:

1
2
3
4
5
6
7
<script type="text/javascript">// <![CDATA[
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com");
}
// ]]></script>

***

6. Using ColdFusion redirect:

1
 

***

7. Using meta refresh redirect:

1
<meta http-equiv="refresh" content="0; url=http://newdomain.com" />

Here “0” is a forward delay in seconds, newdomain.com – is a page, which followed to. Some older browsers do not support meta refresh with a value of 0 for compatibility, you can set a non-zero value.
This redirect will not connect your websites and pass the PR as it is ignored by the search engines. It returns a 200: OK, which corresponds to a regular page. This technique is popular with spammers, so it should be used only for pages that are not to be indexed.

***

8. Using JavaScript redirect

JavaScript redirects allow for a lot of flexibility when it comes to redirects. For example, it is easy to implement a timed delay redirect that forwards visitors to a new web page after a set time. With JavaScript, you can redirect all your visitors to a new URL using the script below:

1
2
3
4
5
<script type="“text/javascript”">// <![CDATA[
<!—
Window.location = http://www.developerdrive.com/
//->
// ]]></script>

To implement a timed delay redirect using JavaScript, use the code below:

1
2
3
4
5
6
7
<script type="“text/javascript”">// <![CDATA[
<!Function delayer() {
	Window.location =../javascriptredirect.php”
}
//- - >
// ]]></script>

Redirection in 5 Seconds!

Please update your bookmarks to reflect our new website!

***

9. CGI PERL Redirect

1
2
$q = new CGI;
print $q-&gt;redirect("http://www.newdomain.com/");

***

10. Ruby on Rails Redirect

1
2
3
4
def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.newdomain.com/"
end

***

11. HTML Redirect (Meta Redirect)

To send someone to a new web page or site put this in the head section of your document:

1
<meta http-equiv="refresh" content="5" />

Here content="5" means the time (e.g. 5) in second the browser should wait before redirecting to new location.

Online Tools

Some online SEO resources can check automatically if your redirects can be crawled correctly and that the redirect was set up right. Once you make a redirection, use these tools (e.g. webconfs.com) to ensure that they are Search Engine friendly.

***

4 Responses

  1. I like this article not only for some useful info on 301 redirects but the inclusion of the various ways that different languages and methods perform their 301 redirects. Thank.

    1. You’re welcome Christian! We’re always glad to provide useful information to our readers!

  2. If you use WordPress there are some nice plugins that make it easy to create 301’s. They can also show you error pages so you can create 301’s on the fly.

Leave a Reply

Your email address will not be published. Required fields are marked *