Setting 301 Redirect - Complete Guide
August 30th, 2007 | by programming |301 Redirect is interpreted as “permanently moved”. Setting a 301 redirect is the best and Search Engine Friendly method for redirecting a webpage. By using 301 redirect, you can move your files around, change file names and redirect your pages without losing your search engine rankings.
301 Redirect in Apache - .htaccess file
1. Redirecting old domain to new domain.
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.yournewdomain.com/$1 [R=301,L]
2. Redirect to “no www” domain:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www.yourdomain.com [nc]
rewriterule ^(.*)$ http://yourdomain.com/$1 [r=301,nc]
3. Redirect to “www” domain:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^yourdomain.com [nc]
rewriterule ^(.*)$ http://www.yourdomain.com/$1 [r=301,nc]
301 Redirect in PHP
Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: http://www.your-new-url.com” );
301 Redirect in IIS
Open Internet Services Manager,then right click on the file or folder you wish to redirect and set target url. Make sure “A permanent redirection for this resource” checkbox is checked.
301 Redirect in Cold Fusion
<.cfheader statuscode="301" statustext="Moved permanently"> <.cfheader name="Location" value="http://www.your-new-url.com">
301 Redirect in CGI-Perl
$q = new CGI;
print $q->redirect("http://www.your-new-url.com/");
301 Redirect in ASP
<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently";
Response.AddHeader("Location","http://www.your-new-url.com/");
%>
301 Redirect in ASP.NET
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.your-new-url.com");
}
</script>
301 Redirect in JSP
<% response.setStatus(301); response.setHeader( "Location", "http://www.new-url.com/" ); response.setHeader( "Connection", "close" ); %>
301 Redirect in Ruby On Rails
def old_action headers["Status"] = "301 Moved Permanently" redirect_to "http://www.new-url.com/" end