IIS Home @ it-notebook.org

Change domain name, redirect all content and keep search engine ranking (and visitors)

(Kristofer Gafvert, July 6, 2010)

Introduction

I recently moved to a new domain name. This has three major drawbacks:

  1. You will not have any benefit of your rankings for the old website.
  2. You will lose visitors, since they won’t find the new website.
  3. You may even be penalized in ranking, because the content on the new website will be a copy of something that already exists on the web.

In this article, I will explain what I did to defeat the drawbacks listed above, by using a custom 404 error page, which looks up the requested URL and maps it to the new URL at the new domain, and redirect the user to the new location.

The reasons for redirecting

The first and most important reason to redirect the old website to the new, is that this will "quick start" your new website. People going to your old website by following links from other websites, will automatically be redirected to the new website, hence your new website will have visitors from day one.

The second reason is that it will notify search engine spiders that you have started a new website, and the old website will be "unpublicized". It should also keep your rankings, which means that you do not need to spend more than necessary to work up your ranking once again. I say "should", because nobody except for the engineers at Google, bing.com, Yahoo and other major search engines. Google do however have the following statement "If you have recently restructured your site or moved to a new domain, pages that previously performed well may now rank poorly. To avoid this, use 301 redirects ("RedirectPermanent")" Source

Three different ways to do redirecting

There are three major ways to do redirecting. All three have their advantages, and also their advantages. I will present the three different ways, and a short explanation.

  • Redirect feature in IIS
    The redirect feature is available in both IIS 6 and IIS 7. It can be fairly complex, but not as flexible as IIS 7 URL Rewrite module. The advantage is that it is available on both editions of IIS.
  • IIS 7 URL Rewrite Module
    This is a powerful tool that can do complex rewriting (redirecting) by using regular expressions. Use this if you run IIS 7 or newer, have a reasonable (folder) structure of the files on both the old and new website, and you are familiar with regular expressions. If you do not have a distinct folder structure, and you more or less need to map specifics files on the old website to their correspondent file on the new website, a better solution is to use a custom 404 error message.
  • Custom 404 error message (which do the redirecting)
    If you do not have distinct folder structure on either the new or old website, you may need to map each individual file. If this is the case, a custom 404 error is a good option; it gives you a good overview of all mappings (if done as in this example). Another major advantage is that setting a custom 404 error message is available at most webhosts, whereas the redirect feature may not. It is also easily migrated in case you need to change IIS server (you do not need to migrate settings in the metabase, it is more or less a copy/paste operation).

Redirect using a custom 404 error message

For this article, I decided to go with a custom 404 error message. The major reason for this was that my webhost does not use IIS 7 (they are still on IIS 6) and I wanted a good and structure view of all the redirections.

The techniques used are ASP.NET 2.0, code-behind, and a simple text file which maps the old URL to the new URL. All files can be downloaded at the end of this article. I will first show and explain the code, followed by instructions how to set this up in IIS.

First follows 404.aspx:

<%@ Page Language="C#" CodeFile="404.aspx.cs" Inherits="Error" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Unknown Title</title>
</head>
<body>
<form id="form1" runat="server">                    
</form>
</body>
</html>

There is really nothing strange with this file. All the actions happen in the code-behind file, which follows now, 404.aspx.cs:

using System;
using System.IO;
using System.Data;
using System.Web;
using System.Xml;
 
    public partial class Error : System.Web.UI.Page
    {
        protected void Page_Load(object sender, System.EventArgs e)
        {
            String queryString = Request.Url.Query.ToString();
            String[] queryStringSplit = queryString.Split(new Char[] {';'});
            
            String failingUrl = queryStringSplit[1];
            
            TextReader textFile = new StreamReader(Server.MapPath("urlmap.txt"));
            
            String lookupString = "";
            String oldUrl = "";
            String newUrl = "";
            
            while ((lookupString = textFile.ReadLine()) != null)
            {                
                oldUrl = lookupString.Split(new Char[] {';'})[0];
                newUrl = lookupString.Split(new Char[] {';'})[1];
                
                if (oldUrl.ToLower() == failingUrl.ToLower())
                {
                    break;
                }                
            }
            textFile.Close();
            if(oldUrl.ToLower() == failingUrl.ToLower())
            {
                //It's a 301
                form1.InnerHtml = "<h1>Object Moved</h1> This document may be found <a href=\"" + newUrl + "\">here</a>";
                Page.Header.Title = "Document Moved";
                Response.Status = "301 Moved Permanently";
                Response.AddHeader("Location", newUrl);
            }
            else
            {
                //It's a 404
                String body = @"<h1>The page cannot be found</h1>
                                Ooops, the page you are looking for is not available.";
                form1.InnerHtml = body;
                Page.Header.Title = "The page cannot be found";
                Response.Status = "404 Not Found";
            }
        }
    }

When our custom 404 error page is called, it passes the failing URL as a query string. So the content of Request.Url.Query.ToString() looks like:

http://www.gafvert.info/404/404.aspx?404;http://www.gafvert.info/missingfile.htm


String[] queryStringSplit = queryString.Split(new Char[] {';'});

The above code splits the string, so we only have the URL we want to redirect.

Next we need to look up the URL, and figure out if it something we should redirect to a new URL, or send back a 404 because it is truly missing.

TextReader textFile = new StreamReader(Server.MapPath("urlmap.txt"));

The rest of the code looks up the URL in urlmap.txt, and figures out if it something that should be redirected to the new website, or something that is missing and a standard 404 should be sent to the client. Important to notice however is that we send an http status code of 301, to indicate to search engine spiders that the requested URL is moved permanently.

Urlmap.txt looks like the following, and is no stranger than a text file with new (before semicolon) and old (efter semicolon) URLS.

http://www.gafvert.info:80/notes/MultipleWebsitesXP.htm;http://www.it-notebook.org/iis/article/multiple_websites_xp.htm
http://www.gafvert.info:80/notes/IISDefaultWebsite.htm;http://www.it-notebook.org/iis/article/create_default_website.htm
http://www.gafvert.info:80/notes/CreateWebSiteIIS6.htm;http://www.it-notebook.org/iis/article/cs_create_website_iis6.htm
http://www.gafvert.info:80/notes/IIS6EncodesCharactersRedirect.htm;http://www.it-notebook.org/iis/article/iis6_encodes_characters_redirect.htm
http://www.gafvert.info:80/notes/WebsiteQuotas.htm;http://www.it-notebook.org/iis/article/website_quotas.htm

Setting this up in IIS is not a difficult thing. Download all three files from the link at the bottom. Unzip the files at C:\inetpub\wwwroot (the root folder of your website).

IIS 6.0

  • Start IIS Manager
  • Right click the website, and click Properties
  • Click on the "Custom Errors" tab
  • Select 404 and click Edit
  • Set "Message type" to URL and set the "URL" to "/404/404.aspx"
  • Click Ok
  • Click Ok
  • Edit urlmap.txt to reflect your old and new website. Note that the old page is before the semicolon, and the new page is to the right of the semicolon.
Set custom error page in IIS 6.0

IIS 7

  • Start IIS Manager
  • Click on the website
  • Go down to "Error pages" (in the IIS group) and double click it
  • Select 404 from the list and click Edit
  • Change to "Execute a URL on this site", and specify "/404/404.aspx"
  • Click Ok
  • Edit urlmap.txt to reflect your old and new website. Note that the old page is before the semicolon, and the new page is to the right of the semicolon.
Set custom error page in IIS 7

If you want to try the custom 404 error locally, you also need to do the following (IIS 7):

  • Click "Edit Feature Settings..."
  • Select "Custom error pages" and click Ok
Enable custom error pages locally in IIS 7

What not to do

Do not use a META Redirect to redirect users to from an old page to a new page, because search engine spiders do not follow META Redirects. Or put in other words, your old page will continue to live in the search engines, hence people will continue to go to the old page, which is not what we want.

In addition to this, don’t just ignore the problem and remove the files on the old website. First of all, this will degrade your ranking, and second, it will make your visitors frustrated. Do not expect visitors to browse thru your website to find the article they are looking for, they will just simply use a search engine to find another website with the very same information.

Applies to [?]

IIS 6.0, IIS 7.0

Resources

Download source code and urlmap.txt
Redirecting using the redirect feature in IIS 6.0