Keeping site root url clean without query string

References

SEO is becoming more and more important issue these days, and your site root url can lose its SEO rank if there are too many different query string attached such as http://www.yoursite.com/?a=b&b=c and http://www.yoursite.com/default.aspx?wt=129. It will be nice you can keep the site root clean lik http://www.yoursite.com

My company employed Scrum from this year and in March sprint, the above was one of the user stories I worked on.

The implementation logic is like this

  • If the url has any query string, store query string values into cookie and redirect it to site root without query string.
  • If there is no query string, then check if cookie has any query string. If the cookie exists, then manually populate Request.QueryString object with the values from cookie.

One tricky bit is Request.QueryString is read only property. So you need to find a private value that the property uses inside. I could do that using .Net reflector and it is _queryString. Using Reflection, you can set the value of a private variable.

The followings are the codes I wrote for this. Of course, I could not write this without the help from the above links.

        protected override void OnInit(EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString.Count > 0)
                    RedirectToCleanSiteRootUrl();

                if(Request.Cookies[“QS”]!= null)
                    PopulateQueryStringFromCookie();
            }

            base.OnInit (e);
        }

        private void RedirectToCleanSiteRootUrl()
        {
            HttpCookie cookie = new HttpCookie(“QS”);
            foreach (string key in Request.QueryString.AllKeys)
            {
                cookie.Values[key] = Request.QueryString[key];
                cookie.Expires = DateTime.Now.AddDays(1d);
            }
            Response.Cookies.Add(cookie);

            PageUtility.RedirectPermanent(“/”);
        }

        private void PopulateQueryStringFromCookie()
        {
            NameValueCollection collection = (NameValueCollection) Request.GetType().GetField(“_queryString”,
                BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
            PropertyInfo readOnlyInfo = collection.GetType().GetProperty(“IsReadOnly”,
                BindingFlags.NonPublic | BindingFlags.Instance);
            readOnlyInfo.SetValue(collection,false,null);

            collection.Add(Request.Cookies[“QS”].Values);
            Response.Cookies[“QS”].Expires = DateTime.Now.AddDays(-1d);
        }

Keeping site root url clean without query string

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 )

Google+ photo

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

Connecting to %s