Sitecore - Remove Port from HTTPS URLs

Sitecore – Remove Port 443 from Sitecore-Generated HTTPS URLs

After we enabled HTTPS on one of our clients’ sites in their pre-prod environment, Sitecore started to add :443 to all URLs, which was undesirable from an SEO perspective. I found this question on Stack Overflow about the issue, and I tried the suggestions of setting scheme="https" and port="443" in the site definition, hoping that Sitecore’s built-in LinkProvider would recognize :443 to be redundant in the URLs, but to no avail.

A trip into the Sitecore 8.1 source code with JetBrains dotPeek revealed that Sitecore’s default LinkProvider will always add either the request URL port or the site definition port to URLs, unless the port number is 80. As far as I could find, there is no logic in the default LinkProvider to remove port 443 when using HTTPS, which is surprising.

Kevin Brechbül’s answer turned out to be the golden ticket, but even after I plugged his custom LinkProvider into Sitecore, Sitecore was still adding :443 to URLs for media hosted in Sitecore.

In this post I’ll outline how to remove port 443 from all URLs generated by Sitecore when using HTTPS.

Create LinkHelper to Remove SSL Port

namespace SitecoreDemo
{
  public static class LinkHelper
  {
    public static string RemoveSslPort(string url)
    {
      if (string.IsNullOrWhiteSpace(url)) return url;
      if (url.StartsWith("https://"))
      {
        url = url.Replace(":443", string.Empty);
      }
      return url;
    }
  }
}

Create a static LinkHelper class to remove port 443 from URLs that start with https. Credit goes to Kevin Brechbül’s answer on Stack Overflow for this code.

Create Custom LinkProvider

using Sitecore.Data.Items;
using Sitecore.Links;

namespace SitecoreDemo
{
  public class NoSslPortLinkProvider : LinkProvider
  {
    public override string GetItemUrl(Item item, UrlOptions options)
    {
      var itemUrl = base.GetItemUrl(item, options);
      return LinkHelper.RemoveSslPort(itemUrl);
    }
  }
}

Create a custom LinkProvider that uses the LinkHelper created above to remove port 443 from all URLs generated by Sitecore.

Create Custom MediaProvider

using Sitecore.Data.Items;
using Sitecore.Resources.Media;

namespace SitecoreDemo
{
  public class NoSslPortMediaProvider : MediaProvider
  {
    public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
    {
      var mediaUrl = base.GetMediaUrl(item, options);
      return LinkHelper.RemoveSslPort(mediaUrl);
    }
  }
}

The custom LinkProvider above will not remove port 443 from URLs generated for Sitecore media, such as images. Create a custom MediaProvider and again use the LinkHelper to remove port 443 from all media URLs generated by Sitecore.

Plug LinkProvider and MediaProvider into Sitecore

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <linkManager>
      <providers>
        <add name="sitecore">
          <patch:attribute name="type">SitecoreDemo.NoSslPortLinkProvider, SitecoreDemo</patch:attribute>
        </add>
      </providers>
    </linkManager>
    <mediaLibrary>
      <mediaProvider>
        <patch:attribute name="type">SitecoreDemo.NoSslPortMediaProvider, SitecoreDemo</patch:attribute>
      </mediaProvider>
    </mediaLibrary>
  </sitecore>
</configuration>

Patch your custom LinkProvider and MediaProvider into Sitecore as shown above.

Final Thoughts

Considering that Google has been boosting sites using HTTPS for almost two years now, it’s surprising that Sitecore adds port 443 to URLs when using HTTPS. Thankfully Sitecore is super extensible, so it’s a piece of cake to get those URLs generated just as we like.

How have you handled removing the port from Sitecore-generated URLs in your projects? Let me know in the comments.

6 responses to “Sitecore – Remove Port 443 from Sitecore-Generated HTTPS URLs”

  1. Thanks Corey for sharing!

    Just a recommendation: the LinkHelper class would be better off being an instance class backed by an interface — static classes introduce tight coupling of code which can cause lots of weeping and gnashing of teeth during maintenance, refactoring, etc. — and inject an instance of it into the NoSslPortLinkProvider and NoSslPortMediaProvider instances via Sitecore Configuration Factory magic.

    Cheers,

    Mike Reynolds

  2. weeder200 says:

    Is this issue specific to 8.1 version ? Didnt see this in earlier versions of 7.2 or 7.5

  3. Jan Sommer says:

    You can cheat the LinkManager by adding port=”443″ externalPort=”80″ to your site-definition in . Don’t know if this will cause other issues though.

  4. Jan Sommer says:

    For the MediaProvider you can remove “:443” in a safer, less verbose way:

    public override string GetMediaUrl(MediaItem item, MediaUrlOptions options) =>
    options.AlwaysIncludeServerUrl ? new Uri(base.GetMediaUrl(item, options)).ToString() : base.GetMediaUrl(item, options);

  5. I have seen in SC 8.2 Update 4 that the base constructor for the MediaProvider is marked obsolete so I believe the following should be added to the code for the updated constructor…
    using Sitecore.Abstractions;

    public NoSslPortMediaProvider(BaseFactory factory)
    : base(factory)
    {
    }

  6. I submitted a comment which would indicate that we would have to investigate using the version of MediaProvider using the base factory mechanism but in doing some additional research using, one Sitecore engineer is suggesting that [obsolete] on the base constructor may have been marked incorrectly… So it may be ok… Wanted to include that comment, just in case 🙂 I will probably tell ReSharper to skip flagging this for this file.

Leave a Reply