Error 301 ((301 Permanent Redirect) returns when accessing a page means that the website has been permanently moved to a new address, it’s also indicated in HTTP header. New visitors and search engine bots will be redirected to a new address. In this case, all characteristics ща old pages will be transferred to the new URL addresses. Making 301 redirect, you bond the old and the new address, parameters like PageRank and Alexa Rank, as well as the weight of the page of the old address will be transferred to the new URL.
Redirect with error code 301 (moved permanently) is the best way to preserve the site’s ranking in search engines when you transfer it to a new domain or change the content management system. Redirecting can be made by several ways, depending on the software installed.
301 redirect in .htaccess
If you are using Apache server, you can simply make redirect 301 using the file .htaccess, in this case, do not forget to include modules mod_alias (to support the directives Redirect, RedirectPermanent and RedirectMatch) and/or mod_rewrite (for rewrite) in php.ini.
Redirect with Redirect directive or Redirect Permanent module mod_alias
Redirect 301 /old-page.html http://new-domain.ru/new-page.html
OR
Redirect permanent /old-page.html http://new-domain.ru/new-page.html
The disadvantage of this method is that you need to list one by one each addresses that you want to redirect. You can also use RedirectPermanent for similar purposes.
RedirectPermanent /old-url.html http://new-site.ru/new-url.html
Redirect with the directive RedirectMatch
This redirection is like the previous one, except that you can specify a regular expression to the old URL. For example, when changing engine from PHP to ASP, you can redirect the old addresses with the help of the next method:
RedirectMatch /(.*)\.php$ /$1.aspx
Redirect using mod_rewrite module RewriteRule directive
To use RewriteRule directive you need to make sure that in httpd.conf connected module mod_rewrite, and the option FollowSymLinks is switched on. Using rewrite module provides many opportunities to redirect pages to new addresses.
Redirect domain from www to non-www
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
or alternative, a more clear syntax
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
Redirect requests from no-www to the domain with www prefix
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
or alternative variant
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.(.*) [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
Redirect with a script (sending headers)
You can also perform redirects with the help of scripts, sending the client the necessary headers.
HTTP/1.1 301 Moved Permanently
Location: http://www.newdomain.ru/newdir/newpage.htm
PHP redirect
<?php
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.newdomain.ru/newdir/newpage.htm”);
exit();
?>
ASP redirect
<%@ Language=VBScript %>
<%
Response.Status=“301 Moved Permanently”
Response.AddHeader “Location”, “http://www.new-url.com”
response.end
%>
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>
ColdFusion redirect
<.cfheader statuscode=“301” statustext=“Moved permanently”>
<.cfheader name=“Location” value=“http://www.new-url.com”>
JSP (Java) redirect
<%
response.setStatus(301);
response.setHeader( “Location”, “http://www.new-url.com/” );
response.setHeader( “Connection”, “close” );
%>
CGI PERL
$q = new CGI;
print $q->redirect(“http://www.new-url.com/”);
Ruby on Rails
def old_action
headers[“Status”] = “301 Moved Permanently”
redirect_to “http://www.new-url.com/”
end
Implementation of the redirect with nginx
if ($host = ‘www.domain.com’ ) {
rewrite ^(.*)$ http://domain.com$1 permanent;
}