Canonical Url e Master page asp

by Michele 9. giugno 2010 14.57

I motori di ricerca quali Google, Yahoo e Bing rilevano durante le indicizzazioni la stessa pagina con url differenti. Questo comporta un errore SEO che potrebbe compromettere la corretta indicizzazione della pagina. Ad esempio gli url:

  • http://nomedominio.com
  • http://www.nomedominio.com
  • http://www.nomedominio.com/default.aspx

si riferiscono alla stessa pagina, ovvero alla home page

 

La soluzione nel caso non si utilizzino master page è molto semplice, basta aggiungere nell'header di pagina l'attributo canonical come segue:

  • <link rel="canonical" href="http://www.nomedominio.it/nomepagina" />

Le cose si complicano quando utilizziamo una master page che comporta l'eredità dell'header su tutte le pagine riferite alla master. Di seguito mostrerò come sono riuscito a risolvere la problematica. Premetto che sono possibili altre soluzioni ma ritengo che questa sia efficace e facile da implementare.

 

  1. Creiamo un classe BasePage.cs a cui faremo riferimento su tutte le pagine dove dobbiamo specificare l'url canonical:

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

namespace MMSoftware.TheMMSoft.UI
{
    public class BasePage : System.Web.UI.Page
    {
      
        private string _canonical;
        // Constructor
        // Add an event handler to Init event for the control
        // so we can execute code when a server control (page)
        // that inherits from this base class is initialized.
        public BasePage()
        {
            Init += new EventHandler(BasePage_Init);
        }

        // Whenever a page that uses this base class is initialized
        // add canonical link if available
        void BasePage_Init(object sender, EventArgs e)
        {

            if (!String.IsNullOrEmpty(Link_Canonical))
            {
                HtmlLink link = new HtmlLink();
                link.Href = Link_Canonical;
                link.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString().ToLower(), "canonical");
                link.Attributes.Add(HtmlTextWriterAttribute.Type.ToString().ToLower(), "");
                link.Attributes.Add("media", "");
                Header.Controls.Add(link);
            }
        }

        /// <summary>
        /// Gets or sets the Link Canonical tag for the page
        /// </summary>
        public string Link_Canonical
        {
            get
            {
                return _canonical;
            }
            set
            {
                _canonical = value;
            }
        }

 
    }
}


Una volta realizzata la classe faremo riferimento alla classe nel codice (behind) della  pagina interessata:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : MMSoftware.TheMMSoft.UI.BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

 

Come potrete vedere la classe di pagina eredita direttamente dalla classe BasePage precedentemente creata.

 

Ora l'ultima operazione che consente di assegnare l'url canonical alla pagina in esame:

 

<%@ Page Title="" Language="C#" MasterPageFile="~/design/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
CodeFileBaseClass="MMSoftware.TheMMSoft.UI.BasePage"

    Link_Canonical="http://www.nomedominio.it/" %>

 

NB: L'IDE non riconosce l'attributo Link_Canonical per questo lo segnala come non riconosciuto anche se ne consente comunque la compilazione. Se volete evitare

questa fastidiosa segnalazione dovrete modificare il seguente file:

C:\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\schemas\html\page_directives.xsd

 

Nella sezione complexType aggiungete la seguente riga:

<xsd:attribute name="Link_Canonical" vs:nonfilterable="true" />

 

Buon lavoro

 

MMSoftware Sistemi Informativi di M.M.


 

 

Tags:

SEO | Tecnologia

Commenti

01/10/2010 14.21.40 #

Cross cutting shredder

Its a pity you dont have a donate button, i would donate some =)

Cross cutting shredder Stati Uniti

02/10/2010 17.07.13 #

michele

Thanks, for comment. If you want, refer to my post or site with links
Bye

michele Italia

04/10/2010 13.40.16 #

Compact refrigerator

Wow. You ARE the greatest.

Compact refrigerator Stati Uniti

04/10/2010 22.57.28 #

michele

spam is not a better way to improvement a link popularity. Please, pay attention

michele Italia

05/10/2010 21.47.59 #

Compact refrigerator

Wow. You ARE the greatest.

Compact refrigerator Stati Uniti

07/10/2010 7.23.44 #

Cross cutting shredder

Where can i find your rss? I cant find it

Cross cutting shredder Stati Uniti

07/10/2010 20.05.07 #

Rachel Sarah Bilson (2) HD Wallpaper

Thanks for posting this. i really enjoyed reading this.

Rachel Sarah Bilson (2) HD Wallpaper Stati Uniti

14/10/2010 23.04.45 #

In Love HD Wallpaper

Its a pity you dont have a donate button, i would donate some =)

In Love HD Wallpaper Stati Uniti

15/10/2010 1.16.01 #

Spiritual forum

This site is great. i visit here evaryday.

Spiritual forum Stati Uniti

15/10/2010 2.09.28 #

How to groom long haired cats how to cut a lion clip on a cat

Great site design!!!! Whattemplate did you use?

How to groom long haired cats how to cut a lion clip on a cat Stati Uniti

Commenti chiusi

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

TextBox

RecentPosts