How to make redirect 301

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

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


Redirect permanent /old-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

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 ^(.*)$$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 ^(.*)$$1 [R=301,L]

or alternative variant

RewriteEngine On

RewriteCond %{HTTP_HOST}     !^www\.(.*)     [NC]

RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

301 redirect

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


 PHP redirect

header(“HTTP/1.1 301 Moved Permanently”);

 ASP redirect

<%@ Language=VBScript %>
Response.Status=“301 Moved Permanently”
Response.AddHeader “Location”, “”

 ASP.NET redirect

<script runat=“server”>
private void Page_Load(object sender, System.EventArgs e)
Response.Status = “301 Moved Permanently”;

 ColdFusion redirect

<.cfheader statuscode=“301” statustext=“Moved permanently”>
<.cfheader name=“Location” value=“”>

 JSP (Java) redirect

response.setHeader( “Location”, “” );
response.setHeader( “Connection”, “close” );


$q = new CGI;
print $q->redirect(“”);

 Ruby on Rails

def old_action
headers[“Status”] = “301 Moved Permanently”
redirect_to “”

 Implementation of the redirect with nginx

if ($host = ‘’ ) {
rewrite ^(.*)$$1 permanent;