here and there during my Sitecore development

Posts tagged “LinkManager

How I make SEO-friendly Sitecore URLs?

In the past, the URLs of your web pages might not be SEO friendly or not self-described enough.  They might look like:

Now with Sitecore, the URL is generated by the structure of items you put in place in the content tree (by default).  If your “Careers” item in Sitecore content tree looks like this:

    • Sitecore
      • Content
        • Home
          • Company
            • About Our Company
              • History
              • Careers ***

The URL of your Careers page will look like this:

www.yourcompany.com/Company/About Our Company/Careers.aspx or
www.yourcompany.com/Company/About%20Our%20Company/Careers.aspx

Note that “%20” is a space character.

Now you notice that the URL is now better that the old style URLs that contain ID or querystrings.

To make it more friendly, we can easily replace space in Sitecore web.config

<encodeNameReplacements>
   <replace mode="on" find="&amp;" replaceWith=",-a-," />
   <replace mode="on" find="?" replaceWith=",-q-," />
   <replace mode="on" find="/" replaceWith=",-s-," />
   <replace mode="on" find="*" replaceWith=",-w-," />
   <replace mode="on" find="." replaceWith=",-d-," />
   <replace mode="on" find=":" replaceWith=",-c-," />
   <replace mode="on" find=" " replaceWith="-" />  <!-- to replace space with dash -->
   <replace mode="on" find="_" replaceWith="-" />  <!-- to replace underscore with dash -->
</encodeNameReplacements>

I added 2 lines to replace both space and underscore with dash

Now the URL of your Careers page should look like this:
www.yourcompany.com/Company/About-Our-Company/Careers.aspx

Take another step, what if you want to remove “.aspx”?  It’s easy…

  1. Change addAspxExtension attribute in web.config to false
    <add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="never" languageLocation="filePath" shortenUrls="true" useDisplayName="false" />
    
  2. You might need to add wildcard script mapping in IIS

Now the URL of your Career page should look better:
www.yourcompany.com/Company/About-Our-Company/Careers

Last, but not least.  Someone said case difference may mean different pages and that case Google would index them separately.  I’m not sure if this is true but the best practice is to have one version of the page (I’d recommend it be the lowercase).  Follow these steps to generate lowercase URLs:Create a new class that overrides Sitecore.Links.LinkProvider class

  1. Create a new class that overrides Sitecore.Links.LinkProvider class
    namespace YourNamespace.Classes.Manager
    {
       // this class override the SitecoreLinkProvider so that the returned URLs will be all lowercase.
       public class CustomFriendlyLinkManager : Sitecore.Links.LinkProvider
       {
          public override string GetItemUrl(Sitecore.Data.Items.Item item, Sitecore.Links.UrlOptions urlOptions)
          {
             urlOptions.SiteResolving = Sitecore.Configuration.Settings.Rendering.SiteResolving;
             return base.GetItemUrl(item, urlOptions).ToLower();
          }
       }
    }
    
  2. In linkManager tag in web.config, replace the Sitecore LinkProvider with your custom Link Provider
    <linkManager defaultProvider="sitecore">
       <providers>
          <clear />
          <!-- comment this out and replace with custom link provider to return URLs with all lowercase -->
          <!--<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="never" languageLocation="filePath" shortenUrls="true" useDisplayName="false" />-->
          <add name="sitecore" type="YourNamespace.Classes.Manager.CustomFriendlyLinkManager , yourprojectname" addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="never" languageLocation="filePath" shortenUrls="true" useDisplayName="false" />
       </providers>
    </linkManager>
    

Now the URL of your Career page looks clean and awesome
www.yourcompany.com/company/about-our-company/careers

Advertisements