Knowledgebase: Tips & Tricks
How do I create a 301 redirect on my site for a page or in a folder?
Posted by Support, Last modified by Support on 12 April 2015 04:33 PM

A 301 redirect is the most efficient and Search Engine Friendly method for web page, web site, or folder redirection. It's not that hard to implement and it should preserve your search engine rankings for that particular page. If you have to change file names or move pages around, it's the safest option. The code "301" is interpreted as "moved permanently".

You can test your redirection with a Search Engine Friendly Redirect Checker.

Below are a couple of methods to implement URL Redirection

ColdFusion Redirect
<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.new-url.com">

PHP Redirect
<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>

ASP Redirect
<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.new-url.com/"
%>

ASP .NET Redirect
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com");
}
</script>

JSP (Java) Redirect
<%
response.setStatus(301);
response.setHeader( "Location", "http://www.new-url.com/" );
response.setHeader( "Connection", "close" );
%>

CGI PERL Redirect
$q = new CGI;
print $q->redirect("http://www.new-url.com/");

Ruby on Rails Redirect
def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.new-url.com/"
end

.htaccess Redirect Old domain to New domain
Create a .htaccess file with the below code, it will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain. The .htaccess file needs to be placed in the root directory of your old website (i.e. the same directory where your index file is placed)

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

Please REPLACE www.newdomain.com in the above code with your actual domain name.

In addition to the redirect I would suggest that you contact every backlinking site to modify their backlink to point to your new website.

Note: This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite module enabled.

.htaccess Redirect to www

Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com
The .htaccess file needs to be placed in the root directory of your old website (i.e. the same directory where your index file is placed)

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

Please REPLACE domain.com and www.newdomain.com with your actual domain name.

Note: This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite module enabled.


Frequently Asked Question:
What's the difference in using a 301 redirect versus a meta redirect?

A Meta Redirect sends someone to a new page (or site) by putting this in the head of your html document:
<meta http-equiv="refresh" content="10;url=http://mynewsite.com/">

Content="10; tells the browser to wait 10 seconds before transfer, choose however long you would like, you can even choose 0 to give a smoother transition, but some (really old) browsers aren't capable of using this so I'd suggest putting a link on that page to your new site for them.

With a meta redirect the page with the redirect issues a 200 OK status and some other mechanism moves the browser over to the new URL. With a 200 OK on both pages, the search engine wants to index both the start page and the target page - and that is a known spam method (set up
10,000 domains full of keywords for the search engines to index then meta redirect the "real visitor" after 0 or 1 seconds to the "real site" ) so using it gets you penalized.

The 301 redirect simply issues a Permanently Moved message in the HTTP header which tells the search engine to only index the target URL.

Conclusion: The safest way to redirect old web pages to the new pages or old web site to the new web site and keep the same search engine rankings is to use the 301 redirect. It will also pass on the page rank from your old site to your new site.

(779 vote(s))
Helpful
Not helpful