Redirect your site with http 301 permenantly to new domain


Redirect your site with http 301 permenantly to new domain

You want to move your site to new domain and dont want to lose your page rank in google search engine.This post has contains some instructions that how to use redirect your site with http 301 permenantly moved status code.

301 means the requested resource has been assigned to a new permanent Uniform Resource Identifier (URI), and any future references to this resource should be done using one of the returned URIs. 

Here is the road map

1)    use  301 Redirect (see httpmodule code in below). and make page to page basis redirection.

2)      Find external links by googling “link:www.yoursite.com”. If some of them has static links to “yoursite”, we must inform the webmasters to change the link.

3)      Track broken links with Xenu (this is an automated tool, that checks all links in a web site and prepare a report about broken links etc.)

4)      Domain www.yoursite.nl should be active for some time (min  180 days) after redirecting is activated.

5)      Add your new site’s url to Google Web Administration Tools.

6)      Prepare a new site map and add this to Google Web Administration Tools.

7)      Check SiteMap details page from time to time, to see how many URL’s google indexed from the new domain.

An example httpModule for redirect request with 301 permenant moved

using System;
using System.Web;
using System.Configuration;
using System.Web.Caching;
using System.Collections.Generic;
 
namespace YourSite
{
    /*
 * This HTTPModule Intercepts the Application Cycle, and performs a 301 Redirect to the domain 
 * parameter given in the web.config file - "newURL"
 */
 
    public class RedirectorModule : IHttpModule
    {
        /// <summary>
        /// You will need to configure this module in the web.config file of your
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members
 
        // The Dictionary that will hold the URLMapping rules
        Dictionary<string, string> URLMapping = new Dictionary<string, string>();
 
        public void Dispose()
        {
            //clean-up code here.
        }
 
        public void Init(HttpApplication context)
        {
            // Below is an example of how you can handle LogRequest event and provide 
            // custom logging implementation for it
            //context.LogRequest += new EventHandler(OnLogRequest);
 
            // INTERCEPT PostResolveRequestCache PHASE
            context.PostResolveRequestCache += new EventHandler(context_PostResolveRequestCache);
        }
 
        void context_PostResolveRequestCache(object sender, EventArgs e)
        {
            // GET APPLICATION AND REQUEST
            HttpApplication app = (HttpApplication)sender;
            HttpRequest request = app.Context.Request;
 
            // TAKE FILE NAME FROM WEB.CONFIG
            string fileName = ConfigurationManager.AppSettings["URLMappingFilePath"];
            string filePath = app.Context.Server.MapPath(fileName);
 
            // CHECK IF URLMAPPING DICTIONARY IS IN CACHE
            if (app.Context.Cache["URLMapping"] == null)
            {
                //
                CacheDependency dependency = new CacheDependency(filePath);
                app.Context.Cache.Insert("URLMapping", URLMapper.DeserializeObject(filePath), dependency);
                URLMapping.Clear();
            }
 
            URLMapping = (System.Collections.Generic.Dictionary<string, string>)app.Context.Cache["URLMapping"];
 
            string lRequestedPath = request.Url.ToString();
            string requestedURL = request.RawUrl.ToString();
 
            if (!CheckFileExtension(requestedURL)) return;
 
            //REMOVE QUESTION MARK AT THE END
            if (requestedURL.Substring(requestedURL.Length - 1).Equals("?"))
                requestedURL = requestedURL.Substring(0, requestedURL.Length - 1);
 
 
            // WHEN USING IN LOCAL WE SHOULD REMOVE APPLICATION NAME FROM URL, 
            // BEFORE WE LOOK TO THE URL MAPPING
            string localAppName = ConfigurationManager.AppSettings["localAppName"];
            if (requestedURL.ToLower().Contains(localAppName.ToLower()))
            {
                requestedURL = requestedURL.Replace(localAppName, "");
            }
 
            if (URLMapping.ContainsKey(requestedURL))
            {
                requestedURL = URLMapping[requestedURL];
            }
 
            string newURL = ConfigurationManager.AppSettings["newURL"];
            string urlToGo = newURL + requestedURL;
 
            app.Response.StatusCode = System.Net.HttpStatusCode.MovedPermanently; // 301
            app.Response.AddHeader("Location", urlToGo);
            app.Response.End();
        }
 
        private bool CheckFileExtension(string requestedURL)
        {
            if (requestedURL.Contains("favicon.ico"))
            {
                return false;
            }
            return true;
        }
 
        #endregion
 
        public void OnLogRequest(Object source, EventArgs e)
        {
            //custom logging logic can go here
        }
    }
}
 
see all Http Status Codes
http://msdn.microsoft.com/en-us/library/aa383887(VS.85).aspx
check your site's page rank
http://www.googlepagerankchecker.com/
For broken links
http://home.snafu.de/tilman/xenulink.html

 

more to see for 301 redirection

http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=93633

 

 
 
 
Advertisement

About bpostaci
Escalation Engineer in Microsoft.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: