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ühl'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ühl'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.