Removing 443 port from HTTPS calls from Sitecore

Another aspect of a Sitecore project I had a chance to work on is a situation involving Sitecore where HTTPS is being but it would add the 443 port number to the URL which is not ideal from an SEO standpoint.

In a nutshell, what I did was create a simple method which examines the URL generated by the override-able GetItemUrl method (good information about this task can be found in Corey Smith’s blog linked here) and ensure that the “:443” port information was stripped from it.

So if you are overriding the GetItemUrl method, you should have a patch configuration file which will be used to inject that update into Sitecore for this process.  An example of the patch file used for this injection is shown below.

<?xml version="1.0" encoding="utf-8" ?>
  <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
    <sitecore>
      <linkManager> 
        <patch:attribute 
          name="defaultProvider" 
          value="CustomLinkProvider" /> 
          <providers> 
            <add name="CustomLinkProvider" 
                 type="MyProject.Services.CustomLinkProvider, 
                       MyProject" 
                  <!-- other configuration options --> 
            /> 
          </providers> 
      </linkManager> 

      <mediaLibrary> 
        <mediaProvider> 
          <patch:attribute name="type"> 
            MyProject.Services.NoSslPortMediaProvider, MyProject 
          </patch:attribute> 
        </mediaProvider> 
      </mediaLibrary>
    </sitecore>
</configuration>

The LinkHelper.cs class which I wrote to trap and remove this port entry is shown below.  I decided to not focus whether an “https” call is made or not but whether the “:443” text shows up at all.

namespace MyProject.Services
{
   /// <summary> 
   /// Link Helper is used to remove SSL Port 
   /// </summary> 
   public static class LinkHelper
   { 
      /// <summary> 
      /// This method removes the 443 port number from url 
      /// </summary> 
      /// <param name="url">The url string being evaluated</param> 
      /// <returns>An updated URL minus 443 port number</returns> 
      public static string RemoveSslPort(string url) 
      { 
         if (string.IsNullOrWhiteSpace(url))
         { 
            return url; 
         }

         if (url.Contains(":443"))
         { 
            url = url.Replace(":443", string.Empty);
         }
 
         return url;
      }
   }
}

An example of how this new class can be used to solve those pesky URL problems is shown below.  This class is patched in using the configuration file mentioned in the beginning of this blog post.

// ReSharper disable ClassNeverInstantiated.Global
// Justification: Referenced in App_Config.
namespace MyProject.Services
{ 
   /// <summary>Provide links for resources.</summary> 
   public class CustomLinkProvider : LinkProvider
   {
      public override string GetItemUrl(Item item, UrlOptions options)
      {
         // Some code which manipulates and exams the item...
 
         return LinkHelper.RemoveSslPort(base.GetItemUrl(item, options));

         // Some more code which looks at the item... 

         return LinkHelper.RemoveSslPort(base.GetItemUrl(item, options));
      }
   }
}

In addition, we wanted to handle media URLs which may also suffer from this problem with 443 ports so the class below was created to handle those URLs and is also referenced in the configuration patch file at the beginning of this blog.

using Sitecore.Data.Items;
using Sitecore.Resources.Media;
#pragma warning disable 612 //deals with [obsolete] constructor test on ReSharper

// ReSharper disable UnusedMember.Global
// Justification: Referenced in App_Config file.
namespace MyProject.Services
{
   /// <summary> 
   /// This method removes SSL port number from Media Item URLs 
   /// </summary> 
   public class NoSslPortMediaProvider : MediaProvider 
   { 
      /// <summary> 
      /// Overrides Url mechanism for Media Items 
      /// </summary> 
      /// <param name="item">Sitecore Media Item</param> 
      /// <param name="options">Sitecore Media Url Options object</param> 
      /// <returns>Updated Media Item URL minus 443 port</returns> 

      public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
      {
         var mediaUrl = base.GetMediaUrl(item, options); 
         return LinkHelper.RemoveSslPort(mediaUrl); 
      } 
   }
}

There is a detail about what I coded  I also wanted to mention.  I am using ReSharper as part of my development environment and I am commenting the third line of this code after the using statements because the parameter-less constructor of the inherited MediaProvider class is marked as obsolete (using an attribute).  I was wondering how I should handle that using its other constructor but then I read the following Sitecore stackexchange blog linked here which seems to suggest that it may be incorrectly marked [obsolete] so I decided to go with that idea but it required me to notify ReSharper to ignore the warning for this specific file which is what that “#pragma” line is doing for me.  That is how I decided to handle it for now until I know for sure what is going on with the obsolete attribute.  This situation actually makes for a good Sitecore support ticket.  In any event I figure I should make you aware of that.  The next comment line is there by the way because our solution does not instantiate this class directly for use.  Sitecore uses it as part of the patch mechanism described earlier so I needed to tell ReSharper to ignore the mechanism which checks to see if a class is not used (through instantiation) so it can be removed from a solution.

So that is about it.  It is great that Sitecore is extensible enough that we are able to fix this issue in our way if necessary.

 

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s