Rendering Fully Qualified Sitecore URLs

Sitecore’s web.config contains many ways to extend and customize the application. One such configurable aspect is dynamic link resolution. This article is intended to explain how you can configure Sitecore to render fully qualified URLs (e.g. http://host/my/path/to/page.aspx) in links.

Make All Links Fully Qualified

Its really easy to configure Sitecore to make all links fully qualified. You can simply update the linkManager configuration and set alwaysIncludeServerUrl to true. Here’s an example:

[xml]

<linkManager defaultProvider="sitecore">
<providers>
<clear />
<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="true" alwaysIncludeServerUrl="true" encodeNames="true" languageEmbedding="never" languageLocation="filePath" shortenUrls="true" useDisplayName="false" />
</providers>
</linkManager>

[/xml]

Only Make Some Links Fully Qualified

If you don’t always want all links to be fully qualified, but rather have certain scenarios when they should be, you can create a custom link provider to handle this. Simply inherit Sitecore.Links.LinkProvider and override the GetItemUrl(...) method:

[csharp]
namespace CustomLibrary.Links
{
public class CustomLinkProvider : Sitecore.Links.LinkProvider
{

public override string GetItemUrl(Sitecore.Data.Items.Item item, Sitecore.Links.UrlOptions options)
{

if (/* my condition of when to apply fully qualified links, e.g. a specific device maybe */)
{

options.AlwaysIncludeServerUrl = true;

}

return base.GetItemUrl(item, options);
}

}
}
[/csharp]

After doing this, change the defaultProvider in the linkManager to your custom class like so:

[xml]

<linkManager defaultProvider="custom">
<providers>
<clear />
<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="true" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="never" languageLocation="filePath" shortenUrls="true" useDisplayName="false" />
<add name="custom" type="CustomLibrary.Links.CustomLinkProvider, CustomLibrary" addAspxExtension="true" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="never" languageLocation="filePath" shortenUrls="true" useDisplayName="false" />
</providers>
</linkManager>

[/xml]

 

Mark Ursino

Mark is Sr. Director at Rightpoint and a Sitecore MVP.

 

12 thoughts on “Rendering Fully Qualified Sitecore URLs

  1. Do you know how to modify it so that when you use the “insert Sitecore link” tool in the Rich Text Editor it supplies a normal url instead of “~/link.aspx?_id=GUID”?

  2. Dominic – that behavior is by design and required. In the RTE in the CMS, items are linked to each other by their GUID, so if you rename the item or move it, the link is still valid (because the GUID is constant). On the front-end of the site though, if you use a FieldRenderer, it will expand that dynamic link into the full URL for the respective item. If you change this behavior, you will break Sitecore!

  3. Here’s my pickle: I would like to link not just to a Sitecore internal page, but to an anchor that exists on that page. So if the page represented by href=”˜/link.aspx?_id=0B16893D260041838FAD668AAA551E21&_z=z” has an anchor named “myanchor”, how can I achieve that?

    1. Through a bit of testing, it appears the solution is simply to add the anchor to the end of the href par usual, so href=”˜/link.aspx?_id=0B16893D260041838FAD668AAA551E21&_z=z#myanchor” works.

  4. Hi Mark,
    We are trying to implement an overrider for the linkProvider but are getting an error – “Error 19 The type ‘System.Configuration.Provider.ProviderBase’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.”

    Any idea why the linkProvider is referencing a v2.0 dll?

    1. Kiran –

      The code in the LinkProvider works based on the configuration, so it needs access to a standard .NET assembly System.Configuration for you to do anything with it. That assembly will be listed as 2.0 if your app pool uses either .NET 2.0 or 3.5 since .NET 3.5 uses the 2.0 kernel.

      1. Hi Mark,
        Sorry for not specifying earlier. So the AppPool uses .net 4.0 and we have a reference to system.configuration in the project, except that it is the 4.0 version. So the issue is the LinkProvider expects a 2.0 version of system.configuration.
        Adding a reference to v2.0 fixes this issue but this is a dirty way around since if we need the 4.0 version elsewhere we would have the use the fully qualified name

    1. That could work too. The method you reference will use the server URL of the page you’re on. The LinkManager approach will instead use the targetHostName of the context Sitecore, regardless of what URL you are on. So say you’re site binds on two hosts: hostName=”www.site.com|www2.site.com” but you want all generated links to prefer www, you would use targetHostName=”www.site.com” and even if you access a page on www2, the links via the LinkManager will use the targetHostName where as the method to refered to will use the host in the address bar.

Leave a Reply to kiran Cancel reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.