301 redirect search engine friendly redirect

I am describing here how to setup 301 redirect in various servers. We have posted various helpful articles and tools on our site. We keep getting requests from people for an article on making redirects search engine friendly. When you are moving one page to other or moving site from http to https or fighting canonical issues 301 redirect is the way to go.

301 redirect search engine

301 redirect setup is not same for different servers. Each server works in a different way. For IIS servers you need to make changes directly on the server. Mostly you need dedicated server or remote server access. But for other servers it can be done just through FTP. Here are some sample coding for important servers.

If you are moving pages it is important to keep your rankings intact. Remember search engines rank pages not websites. You need to make sure none of the pages have 2 versions because it will create duplicate issue. Learning about search engine friendly redirects is the way to go.

Apache Server

Doing redirect in php files is a simple way to do on an apache server if you want to redirect only one page to an other page.

Here is a sample how to redirect in PHP
<?
Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: http://www.example.com” );
?>

Adding redirect in .htaccess is another efficient way to redirect in an Apache server. .htaccess is an efficient file used for both server access and SEO purposes. Most of URL rewriting , URL redirects can be done in .htaccess file.

htaccess redirect

There are many types of redirects you can do using htaccess

To redirect from one domain to another domain. There are various reasons you want to redirect from one domain to another domain. You want to move your site to a new domain name, you want to just replace existing domain name for other purpose but use new domain as main site. Like that there might be many uses to redirect from one domain to another domain name. You can do that in .htaccess file. .htaccess is a server file so handle with care. If you don’t know how to handle it leave it to an expert. You can create a htaccess file with your notepad. Just past this below code in notepage and save it as .htaccess.

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

Just replace yournewdomain.com with the URL you want to redirect.

Search engines like Google used to have problems figuring out the difference between www and non-www versions of a site. John Muller even suggested verifying both versions in webmaster tools to see data. This shows Google is still struggling with www and non-www version of a site. We have an option in webmaster tools to select which version we want to use. We can select either of the 2 versions. But to avoid confusion and play safe you can just 301 redirect from one version to the other. This way Google will not have problem identifying the right version to use.

Just include the below code in your .htaccess file to redirect all requests to example.com to be redirected to www.example.com

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

Just change example.com in the above code to your own site URL. That’s boom you are done fixing www and non www versions.

Remember .htaccess works only on Apache servers. Just because PHP can run in IIS and other servers htaccess will work. htaccess doesn’t have anything to do with PHP. It is an apache server file.

Windows Server.

Redirect using ASP pages. Sample like PHP pages you can redirect from one page to another page in IIS server using this simple ASP code. Just include this code in the top part of your ASP page.

<%@ Language=VBScript %>
<% Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”,”http://www.example.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.example.com”);}
</script>

PERL

Below code will help you redirect in a perl server code. Copy the below code and replace example.com with your own code.

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

Simple 301 redirect on Ruby on Rails

def old_action
headers[“Status”] = “301 Moved Permanently”
redirect_to “http://www.example.com/”
end

JSP server

Java Server Pages an important serve side code used in Java enabled servers. Many sites use Java as their backend platform and they will need a SEO friendly redirect if they are moving one page from the other. Copy the below code to your server and it should work best for your SEO purpose.

JSP (Java) Redirect
<%
response.setStatus(301);
response.setHeader( “Location”,”http://www.example.com/” );
response.setHeader( “Connection”,”close” );
%>

IIS server

Setting up redirect in IIS is the most difficult part. When I first started doing SEO I always found it difficult to setup redirect in IIS. We use to buy a simple software called ISAPI redirect. But things have changed now. It is much easier to setup 301 redirect on server side. If you have access to a dedicated server through remote server access you can do the following edit. It is simple and you don’t need any programming skills to do it. Just loggin to your remote server.

  1. Go to Internet Services Manager and right click on the folder you want to setup a redirect.
  2. Select the radio titled “a redirection to a URL”.
  3. Enter the redirection page
  4. Check “The exact url entered above” and the “A permanent redirection for this resource”
  5. Click on ‘Apply’

Cold Fusion Server

There are couple of methods you can use to setup cold fusion redirect.

First method is to use

CFlocation. You can use both cflocation and cfheader.

Just add this below code on the page you want to redirect. Replace newpage.cfm to your own page name or page.

<cflocation url=”newpage.cfm” statuscode=”301″ addtoken=”false”>

Using CFheader just use this below code.
<cfheader statuscode=”301″ statustext=”Moved permanently”>
<cfheader name=”Location” value=”http://www.example.com/newpage.cfm”>

Redirect from http to https on an Apache server

With Google’s new algorithm tweak which helps https sites, there is rush to setup https and redirect http pages to https. I will write a seperate article on this. For now here is a simple example how to redirect on an Apache server.

Apache recommends using RedirectSSL as the preferred method. But to use that method we need to have direct access. For shared hosting it is not possible to gain direct access. So I recommend using .htaccess redirect.

To redirect your whole site to https use this code. This code will redirect which ever page being opened to be redirected to the https version of it. For example www.example.com will redirect to https://www.example.com

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

This code can be used to redirect a specific page or directory.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?secure/(.*) https://%{SERVER_NAME}/secure/$1 [R,L]

If you have questions email me at webpromotions@gmail.com I am more than willing to help if I have enough time in my hands.

No comments yet.

Leave a comment

Request a Free SEO Quote