How do you create a 301 redirect that keeps your Google ranking?

Use a 301 redirect to send an old web address to a new one without losing your ranking in Google: it is a search-engine-friendly redirect that preserves your SEO value. Below you’ll find what you need and ready-to-use examples for .htaccess, PHP, ASP, ASP.NET, Python and Perl.

What is a 301 redirect?

A 301 redirect tells browsers and search engines that a page has moved permanently. Visitors are sent to the new address automatically, and search engines transfer the page’s ranking value to the new location. This is the recommended way to redirect traffic without hurting your search visibility.

What you need

To create a 301 redirect you need a hosting account where you can work with .htaccess files – for example a Loopia website on the UNIX platform. If you only have a LoopiaDNS account (no hosting), a simpler preset forwarding option is available in the Loopia Customer Zone instead, though it can’t be customised manually with .htaccess.

Edit .htaccess – choose a plain-text editor

To create a redirect in an .htaccess file you need a text editor that doesn’t add formatting. Recommended options:

When you save the file, use “Save as” and enter the file name “.htaccess” – note the dot at the start. On a Mac the file may seem to disappear, because macOS treats files starting with a dot as system files and hides them.

Common examples

Below are the six most common ways to create a 301 redirect, depending on your platform. Before using these examples, your domain needs to be configured as “Website with Loopia” and a Unix version, except for ASP and ASP.NET, where you choose “Windows (.NET, ASP)” instead.

.htaccess

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^/?(.*)$ https://www.yournewdomain.com/$1 [L,QSA,R=301]
RewriteRule ^/?$ https://www.yournewdomain.com/ [L,QSA,R=301]

Replace “yournewdomain.com” with the address to redirect to, and save the file as “.htaccess” in public_html.

PHP

<?php
header( "HTTP/1.1 301 Moved Permanently" );
header( "Status: 301 Moved Permanently" );
header( "Location: https://www.yournewdomain.com" );
?>

Replace “yournewdomain.com” with the address to redirect to, and save as “index.php”.

ASP

<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","https://www.yournewdomain.com/"
%>

Replace “yournewdomain.com” with the address to redirect to, and save as “default.asp”.

ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" %>
<%
Response.Status = "301 Moved Permanently";
Response.StatusCode = 301;
Response.AddHeader("Location","https://yournewdomain.com");
%>

Replace “yournewdomain.com” with the address to redirect to, save as “Default.aspx” and upload via FTP to the “WWWRoot” subfolder of your domain’s home directory.

Python

#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-
print("Content-type: text/html")
print("Status: 301 Moved Permanently")
print("Location: https://yournewdomain.com/")
print()

Save as “index.cgi” in the public_html folder of the domain you are redirecting from, set the permissions to 755, and add the following to .htaccess:

Options +ExecCGI
AddHandler cgi-script .cgi
DirectoryIndex index.cgi

Perl

#!/usr/local/bin/perl
use strict;
use warnings;

print "Content-Type: text/html\n";
print "Status: 301 Moved Permanently\n";
print "Location: https://yournewdomain.com/\n\n";

Save as “index.cgi” in the public_html folder of the domain you are redirecting from, set the permissions to 755, and add the following to .htaccess:

Options +ExecCGI
AddHandler cgi-script .cgi
DirectoryIndex index.cgi

CGI scripts at Loopia use the .cgi file extension – see the Perl and Python CGI guides for the full requirements.

Was this article helpful?

Related Articles