joomla visitors
Mithila Software Blog | Fixing BlogEngine.Net For SEO Requirements

Fixing BlogEngine.Net For SEO Requirements

September 06, 2011 Posted by Mithila Software

After the Panda updates the onsite seo has taken a front seat.The html suggestions that you see in your Google Webmaster Tools must be taken care of.Talking about a blog engine.net based site we have some conditions that needs to be fixed.These changes have to be done in be_intallation_location/themes/site.master.cs

Steps :

1.Remove unnecessary tagged pages from google index.


2.301 Redirect duplicate pages occured due to Case problem of IIS (Example Mobile-Review.aspx & mobile-review.aspx)

 

3.Remove paged urls from index except the main category url.

 

4.Remove duplicate post url from index bearing date in url (eg - yyyy/mm/dd)

 

5.Adding Canonical tag to duplicate post pages.



Google webmaster suggests these sample urls have problem

/?tag=/32-mega-pixel-ericsson-phones
http://www.xx.com/post/2009/10/28/Dodge-Ram-1500-mpv-review.aspx

/category/aa-Review.aspx
/category/AA-review.aspx

/category/aa-Review.aspx?page=5
/category/AA-review.aspx?page=5


And this one is the fix that you can extend

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
using System.Text.RegularExpressions;

public partial class site : System.Web.UI.MasterPage
{
  protected void Page_Load(object sender, EventArgs e)
  {
        if (Page.User.Identity.IsAuthenticated)
        {
            aLogin.InnerText = Resources.labels.logoff;
            aLogin.HRef = BlogEngine.Core.Utils.RelativeWebRoot + "login.aspx?logoff";
        }
        else
        {
            aLogin.HRef = BlogEngine.Core.Utils.RelativeWebRoot + "login.aspx";
            aLogin.InnerText = Resources.labels.login;
        }

        if (Request.QueryString.Count == 0 && Request.RawUrl.Replace("?","").EndsWith("/default.aspx",StringComparison.OrdinalIgnoreCase))
        {
            string query = Request.QueryString["theme"];
            string theme = !string.IsNullOrEmpty(query) ? query : BlogSettings.Instance.Theme;
            string path = string.Concat(Utils.RelativeWebRoot, "themes/", theme, "/PostViewFrontPage.ascx");
            //Control.MapPath() 

            PostViewBase postView = (BlogEngine.Core.Web.Controls.PostViewBase)LoadControl(path);
            postView.ShowExcerpt = true;// BlogSettings.Instance.ShowDescriptionInPostList;
            //just set a temp post to show - for not getting error
            Post tmp = BlogEngine.Core.Post.Posts[0];
            postView.Post = tmp;
            postView.ID = "sdfsdfsf";// tmp.Id.ToString().Replace("-", string.Empty);
            postView.Location = ServingLocation.PostList;

            fronttop.Controls.Add(postView);
        }
        
        
         string rawUrl = String.Concat(this.GetApplicationUrl(), Request.RawUrl);
        if (rawUrl.Contains("/post/"))
        {
            bool hasQueryStrings = Request.QueryString.Keys.Count > 1;

            if (hasQueryStrings)
            {
                Uri uri = new Uri(rawUrl);
                rawUrl = uri.GetLeftPart(UriPartial.Path);

                HtmlLink canonical = new HtmlLink();
                canonical.Href = rawUrl;
                canonical.Attributes["rel"] = "canonical";
                Page.Header.Controls.Add(canonical);
            }

            Regex regex = new Regex(@"^http(s)?://([\w-]+\.)+[/[\w- ./]+[/[\w- ./]+[\-\/\s]\d{4}[\-\/\s]?((((0[13578])|(1[02]))[\-\/\s]?(([0-2][0-9])|(3[01])))|(((0[469])|(11))[\-\/\s]?(([0-2][0-9])|(30)))|(02[\-\/\s]?[0-2][0-9]))+(/[\w- ./?%&=]*)?$");
            bool hasDateUrl = regex.IsMatch(rawUrl);
            if (hasDateUrl)
            {
                HtmlGenericControl meta = new HtmlGenericControl("meta");
                meta.Attributes["name"] = "robots";
                meta.Attributes["content"] = "noindex,follow";
                Page.Header.Controls.Add(meta);
            
            }

        }


        if (rawUrl.Contains("?tag="))
        {         
                HtmlGenericControl meta = new HtmlGenericControl("meta");
                meta.Attributes["name"] = "robots";
                meta.Attributes["content"] = "noindex,follow";
                Page.Header.Controls.Add(meta);
        }


        if (rawUrl.Contains("/category/"))
        {
            bool hasQueryStrings = Request.QueryString.Keys.Count > 1;

            if (hasQueryStrings)
            {

                HtmlGenericControl meta = new HtmlGenericControl("meta");
                meta.Attributes["name"] = "robots";
                meta.Attributes["content"] = "noindex,follow";
                Page.Header.Controls.Add(meta);

            }

            else
            {

                bool hasCapitalUrl = false;
                foreach (Char C in rawUrl)
                {
                    if (Char.IsUpper(C))
                        hasCapitalUrl = true;
                }

                if (hasCapitalUrl)
                {
                    Response.StatusCode = 301;
                    Response.Status = "301 Moved Permanently";
                    Response.AddHeader("Location", rawUrl.ToLower());
                
                }

            }

        }
        
  }
  
   private string GetApplicationUrl()
    {
        string basePath;
        string port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
        string protocol = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
        string serverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];

        if (port == null || port == "80")
        {
            port = String.Empty;
        }
        else
        {
            port = String.Concat(":", port);
        }

        if (protocol == null || protocol == "0")
        {
            protocol = "http://";
        }
        else
        {
            protocol = "https://";
        }

        basePath = String.Concat(protocol, serverName, port);
        return basePath;
    }

}
blog comments powered by Disqus
Address ::
Mithila Software Solutions,
Green House , 3 GM Road ,
Near Radio Station , Darbhanga
PIN:: 846004
Phone :: # 094-70-625230