soultricks.com

How to remove duplicate title and description meta tags in BlogEngine.net

INTERNET
Who am I
Lluis Enric Mayans
@lluisenricmayans
EXTERNAL REFERENCES:
CONSULTED SOURCES:
Content warning:
Article evaluation:

For the purpose of a good ranking in search engines, Google continues to give a lot of importance to title and description tags.

The structure of Blogs today is based on the dynamic display of a list of content represented in reverse chronological order.

The list of posts, in fact, can be consulted by browsing, through the navigation links, the Home Page, the Categories section, Tags and the list of Months, this generates different pages in terms of content but with the same title and description with consequent penalization from part of Google.



To find a solution to this problem, you need to act on the file default.aspx.cs of BlogEngine.Net, going to modify the code and trying to assign a unique title to each page and to each specific section, category, home, tag, etc., a reference description of not short length, as Google also reports as an error the so-called short meta descriptions.

Let's analyze the code and see the changes made.

As for the Home Page, I had to customize the title tag in order to give each page chosen within the default.aspx a unique title:

 

protected void Page_Load(object sender, EventArgs e) { bool DescriptionAdded = false; if (Page.IsCallback) return; BlogEngine.Core.Page frontPage = BlogEngine.Core.Page.GetFrontPage(); if (Request.QueryString.Count == 0 && frontPage != null) { Server.Transfer(Utils.RelativeWebRoot + "page.aspx?id=" + frontPage.Id); } else if (Request.RawUrl.ToLowerInvariant().Contains("/category/")) { DisplayCategories(); DescriptionAdded = true; } else if (Request.RawUrl.ToLowerInvariant().Contains("/author/")) { DisplayAuthors(); DescriptionAdded = true; } else if (Request.RawUrl.ToLowerInvariant().Contains("?tag=")) { DisplayTags(); DescriptionAdded = true; } else if (Request.QueryString["year"] != null || Request.QueryString["date"] != null || Request.QueryString["calendar"] != null) { if (Request.RawUrl.Contains("year=")) Redirect(); else DisplayDateRange(); } else if (Request.QueryString["apml"] != null) { DisplayApmlFiltering(); } else { PostList1.ContentBy = ServingContentBy.AllContent; PostList1.Posts = Post.Posts.ConvertAll(new Converter(delegate(Post p) { return p as IPublishable; })); if (!BlogSettings.Instance.UseBlogNameInPageTitles) Page.Title = BlogSettings.Instance.Name; if (Request.QueryString["page"] != "1" && Request.QueryString["page"] != null) { Page.Title += " - Pagina " + Request.QueryString["page"]; } } AddMetaKeywords(); if (!DescriptionAdded) base.AddMetaTag("description", Server.HtmlEncode(BlogSettings.Instance.Description)); base.AddMetaTag("author", Server.HtmlEncode(BlogSettings.Instance.AuthorName)); }


To avoid duplicates of Title and Description, I also intervened in the Categories, Tags, Authors and Date sections.


This is what the old code was like regarding Categories:

 

private void DisplayCategories() { if (!String.IsNullOrEmpty(Request.QueryString["id"])) { Guid categoryId = new Guid(Request.QueryString["id"]); PostList1.ContentBy = ServingContentBy.Category; PostList1.Posts = Post.GetPostsByCategory(categoryId).ConvertAll(new Converter(delegate(Post p) { return p as IPublishable; })); Page.Title = Category.GetCategory(categoryId).Title;

 

The new code, on the other hand, will allow us with Base.AddMetaTag to customize the description according to the chosen category and with the Page.Title, in addition to the chosen page number, to customize the title for each page that we will scroll. This way we will avoid duplicates for the title tag and a few fewer duplicates for the description tag:

 

private void DisplayCategories() { if (!String.IsNullOrEmpty(Request.QueryString["id"])) { Guid categoryId = new Guid(Request.QueryString["id"]); PostList1.ContentBy = ServingContentBy.Category; PostList1.Posts = Post.GetPostsByCategory(categoryId).ConvertAll(new Converter(delegate(Post p) { return p as IPublishable; })); Page.Title = Category.GetCategory(categoryId).Title + " - " + BlogSettings.Instance.Name; if (Request.QueryString["page"] != "1" && Request.QueryString["page"] != null) { Page.Title += " - Pagina " + Request.QueryString["page"]; } base.AddMetaTag("description", "Dal Blog SoulTricks, un elenco degli articoli che appartengono alla categoria " + Category.GetCategory(categoryId) + ""); } }

 

Same thing goes for the code regarding Authors, the Tags, and the month e year choice:


 

private void DisplayAuthors() { if (!string.IsNullOrEmpty(Request.QueryString["name"])) { string author = Server.UrlDecode(Request.QueryString["name"]); PostList1.ContentBy = ServingContentBy.Author; PostList1.Posts = Post.GetPostsByAuthor(author).ConvertAll(new Converter(delegate(Post p) { return p as IPublishable; })); Page.Title = "Gli articoli di " + Request.QueryString["name"] + " - " + BlogSettings.Instance.Name; if (Request.QueryString["page"] != "1" && Request.QueryString["page"] != null) { Page.Title += " - Pagina " + Request.QueryString["page"]; } base.AddMetaTag("description", "Dal Blog SoulTricks, un elenco di tutti gli articoli scritti da " + Request.QueryString["name"]); } } private void DisplayTags() { if (!string.IsNullOrEmpty(Request.QueryString["tag"])) { PostList1.ContentBy = ServingContentBy.Tag; PostList1.Posts = Post.GetPostsByTag(Request.QueryString["tag"].Substring(1)).ConvertAll(new Converter(delegate(Post p) { return p as IPublishable; })); base.Title = " All posts tagged '" + Request.QueryString["tag"].Substring(1) + "'" + " - " + BlogSettings.Instance.Name; if (Request.QueryString["page"] != "1" && Request.QueryString["page"] != null) { Page.Title += " - Pagina " + Request.QueryString["page"]; } base.AddMetaTag("description", "Dal Blog di SoulTricks, un elenco di tutti gli articoli classificati con tag " + Request.QueryString["tag"].Substring(1) + ""); } } private void DisplayDateRange() { string year = Request.QueryString["year"]; string month = Request.QueryString["month"]; string specificDate = Request.QueryString["date"]; if (!string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(month)) { DateTime dateFrom = DateTime.Parse(year + "-" + month + "-01", CultureInfo.InvariantCulture); DateTime dateTo = dateFrom.AddMonths(1).AddMilliseconds(-1); PostList1.ContentBy = ServingContentBy.DateRange; PostList1.Posts = Post.GetPostsByDate(dateFrom, dateTo).ConvertAll(new Converter(delegate(Post p) { return p as IPublishable; })); Page.Title = dateFrom.ToString("MMMM yyyy") + " - " + BlogSettings.Instance.Name; if (Request.QueryString["page"] != "1" && Request.QueryString["page"] != null) { Page.Title += " - Pagina " + Request.QueryString["page"]; } base.AddMetaTag("description", "Blog a carattere sociale con news dal web, aperto a tutti coloro che vogliono condividere un loro pensiero. " + Title + ""); } else if (!string.IsNullOrEmpty(year)) { DateTime dateFrom = DateTime.Parse(year + "-01-01", CultureInfo.InvariantCulture); DateTime dateTo = dateFrom.AddYears(1).AddMilliseconds(-1); PostList1.ContentBy = ServingContentBy.DateRange; PostList1.Posts = Post.GetPostsByDate(dateFrom, dateTo).ConvertAll(new Converter(delegate(Post p) { return p as IPublishable; })); ; Page.Title = dateFrom.ToString("yyyy") + " - " + BlogSettings.Instance.Name; if (Request.QueryString["page"] != "1" && Request.QueryString["page"] != null) { Page.Title += " - Pagina " + Request.QueryString["page"]; } base.AddMetaTag("description", "Blog a carattere sociale con news dal web, aperto a tutti coloro che vogliono condividere un loro pensiero. " + Title + ""); } else if (!string.IsNullOrEmpty(specificDate) && specificDate.Length == 10) { DateTime date = DateTime.Parse(specificDate, CultureInfo.InvariantCulture); PostList1.ContentBy = ServingContentBy.DateRange; PostList1.Posts = Post.GetPostsByDate(date, date).ConvertAll(new Converter(delegate(Post p) { return p as IPublishable; })); ; Page.Title = date.ToString("MMMM d. yyyy ") +" - "+ BlogSettings.Instance.Name; if (Request.QueryString [" page "]! =" 1 "&& Request.QueryString [" page "]! = null) {Page.Title + =" - Page "+ Request.QueryString [" page "];} base.AddMetaTag (" description "," Social blog with news from the web, open to all those who want to share their thoughts. " + Title + ""); } else if (!string.IsNullOrEmpty(Request.QueryString["calendar"])) { calendar.Visible = true; PostList1.Visible = false; Page.Title = Server.HtmlEncode(Resources.labels.calendar) + " - " + BlogSettings.Instance.Name; if (Request.QueryString["page"] != "1" && Request.QueryString["page"] != null) { Page.Title += " - Pagina " + Request.QueryString["page"]; } base.AddMetaTag("description", "Blog a carattere sociale con news dal web, aperto a tutti coloro che vogliono condividere un loro pensiero.


 


These changes to the default.aspx.cs file allow you to have unique titles for each page while as regards the description, you have the uniqueness only for the individual sections (Home, Categories, Tags, Authors, Dates).

With a few more changes it is also possible to customize the description for each page belonging to the sections indicated, but with the suggestions proposed we can already get those improvements that Google would certainly like.


This is the new zipped file of default.aspx.cs (11.46 kb) to replace the one you already have in the root of your blog (always make a backup of the existing file first).

Attention because the proposed solution has been tested with BlogEngine.net 2.5

See you next

Add a comment from How to remove duplicate title and description meta tags in BlogEngine.net
Comment sent successfully! We will review it in the next few hours.