HaikoFalk
Software Engineer

Sitecore LinkManager: SEO optimized links in multisite solutions


For SEO optimized links on your website you should build uniform links. For example all URLs should be lower case. Also they should only contain a language code if you really have more than one language. But how to do this if you have several pages in one Sitecore solution and each with different requirements?

So your URL looks like this if you have only one language: http://www.foobar.com and like this if you have two: http://www.foobar.com/en-us/ and http://www.foobar.com/de-de/.

This is quite easy to do with the Sitecore LinkManager. But if you have a multisite Sitecore solution and you need a different LinkManager for some sites you have to customize a bit.

In my example we have sites with the language information in the URL and sites without – so two different LinkManager configurations.

LinkManger configuration

First I have patched the LinkManager, using the Sitecore config transformations during startup.


<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">

    <sitecore>

        <linkManager defaultProvider="sitecore">

            <patch:attribute name="defaultProvider">switch</patch:attribute>

            <providers>

                <add name="sitecore" languageEmbedding="asNeeded">

                    <patch:attribute name="languageEmbedding">never</patch:attribute>

                </add>

                <add patch:after="add[@name='sitecore']" name="language" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" […] languageEmbedding="always" languageLocation="filePath" […] />

                <add patch:after="add[@name='sitecore']" name="switch" type="Uponor.Web.SitecoreExtensions.Providers.LinkProviderSwitch, Uponor.Web.SitecoreExtensions" />

            </providers>           

        </linkManager>

    </sitecore>

</configuration>

As you can see, I declare the “switch“ as the default LinkManager provider. The LinkManager “sitecore” is never embedding the language. The “language” LinkManager configuration is added and always shows the language in the URL. The order is here of no meaning.

Implementation of the “switch” provider

The code in the “switch” configuration is an elementary part of the solution. The class inherits Sitecore.Links.LinkProvider. All public methods of the LinkProvider class are overwritten here.

For example:


public override LanguageEmbedding LanguageEmbedding

        {

            get { return GetContextProvider().LanguageEmbedding; }

        }

The method “GetContextProvider()” is the key feature. It checks if the configuration of the site contains a defined hint to an alternative LinkManager configuration. If so, it returns the custom LinkProvider, if not the default.


private LinkProvider GetContextProvider()

        {

            // default is always the original sitecore provider

            LinkProvider result = LinkManager.Providers["sitecore"];

            if (Context.Site != null)

            {

                String providerName = Context.Site.GetLanguageProvider();

                // custom provider configured and existing?

                if (!string.IsNullOrEmpty(providerName) && LinkManager.Providers[providerName] != null)

                {

                    result = LinkManager.Providers[providerName];

                }

            }   

            return result;

        }

The called method GetLanguageProvider()returns the name of the LinkManager configuration and is a helper that reads the property from the site configuration.


site.Properties["languageProvider"]

Site configuration

The last thing to do is adjusting the site configuration by just adding the “languageProvider” attribute:


<site name="foobar" patch:before="site[@name='modules_website']" languageProvider="language" […] />

Now you can configure each site separately and get different URLs just as you need them.

Of cause I found some inspiration in the internet for my solution. Mainly http://sitecoreskills.blogspot.de/2014/02/a-switching-link-provider.html helped me to implement this solution.

Thanks to Martin Davies ;)

Kommentare
Es wurden noch keine Kommentare zu diesem Eintrag geschrieben.
Kommentar hinzufügen
Vor und Zuname
E-Mail
E-Mail bei weiteren Kommentaren
Mein Kommentar