0

I have an API in Sitecore that takes content from Sitecore and provides it to an external non-Sitecore site. While retrieving content, the API renders a rich text field using a glass mapper. This rich text content may contain "internal links" back to my Sitecore sites. Some of these internal links are rendered as absolute and some as relative links depending on which Sitecore site I am linking to. But any URLs in that content that are relative will fail on the external site. How do I force either Glass Mapper or Sitecore's Field Renderer to always render absolute URLs? I don't mind if this is a global setting.

More context

I have two domains in Sitecore with A and B. The API and main site run on domain A. If I link to site-A in my rich text field the URL renders as relative. Perhaps Sitecore sees this as the same site link. However, if I link to a page on site-B in my rich text field, since the API is hosted on site-A. Sitecore is smart enough to render this site-B link as an absolute URL since it's going across domains. What I'd like to do is always render absolute URLs regardless of where the link is going. Because my external site won't handle relative URLs well. I guess I could solve this with post-processing. Find and replace any relative URLs after the fact with the site-A domain. But I was wondering if there is a more "Sitecore" way to solve this.

0

The out of the box solution is in the Sitecore.config file. You need to set the alwaysIncludeServerUrl setting to true on the LinkProvider.

<linkManager defaultProvider="sitecore">
    <providers>
      <clear />
      <add name="sitecore" 
           type="Sitecore.Links.LinkProvider, Sitecore.Kernel" 
           addAspxExtension="false" 

           alwaysIncludeServerUrl="true"

           encodeNames="true" 
           languageEmbedding="asNeeded" 
           languageLocation="filePath" 
           lowercaseUrls="false" 
           shortenUrls="true" 
           useDisplayName="false" />
    </providers>
  </linkManager>

However, if you have overridden the default link provider with your own, this may not work. You need to look at your custom link provider code to see what is going on. If you call base.GetItemUrl(item, options) within your custom link provider, you can simply set options.AlwaysRenderServerUrl to true in order to force absolute urls globally.

public class CustomLinkProvider : LinkProvider
{
    public override string GetItemUrl(Item item, UrlOptions options)
    {
        Assert.ArgumentNotNull(item, nameof(item));
        Assert.ArgumentNotNull(options, nameof(options));



        // Your custom link provider logic here (url slugs, custom aliases, etc.)



        // Fallback to out of the box logic with absolute urls
        options.AlwaysIncludeServerUrl = true;
        url = base.GetItemUrl(item, options);
        if (string.IsNullOrEmpty(url))
            return string.Empty;

        // Test your solution out first, but you may want to remove default ports from urls if they show up in links.
        url = url.Replace(":80", string.Empty);
        url = url.Replace(":443", string.Empty);
        return url;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.