3

In a site setting you can set the scheme for the site. i.e. Scheme=https

<site name="website">
   <patch:attribute name="scheme" value="https"/>
</site>

The item Link Provider obeys the scheme setting from the site config. So if it is set to https, then all links will be https if AlwaysIncludeServerUrl is set to true in the link provider settings.

But the mediaProvider does not do the same. The mediaProvider only sends back the same scheme that was sent on the request. So if I request a page https://mysite, then all images will be https. But if I request the same site in http://mysite, the scheme is ignored. All links will be http.

Any way to make Sitecore obey the scheme on media also?

3

Sitecore support

Sitecore recorded this as a bug.

Patch can be found here https://github.com/SitecoreSupport/Sitecore.Support.170025

Self rolled code

I was able to resolve this with code

Hacky class to make the switch

public static string ConvertHttpToHttps(string url)
{
    if (Sitecore.Context.Site?.SiteInfo?.Scheme == "https") 
    {
        if (string.IsNullOrWhiteSpace(url)) return url;
        if (url.StartsWith("http://"))
        {
            url = url.Replace("http://", "https://");
        }
    }

    return url;
}

The new media provider

namespace Client.Core.MediaProvider
{
#pragma warning disable CS0612 // Type or member is obsolete
    public class NoSslPortMediaProvider : Sitecore.Resources.Media.MediaProvider
    {
        public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
        {
            var mediaUrl = base.GetMediaUrl(item, options);
            return LinkHelper.ConvertHttpToHttps(mediaUrl);
        }
    }
#pragma warning restore CS0612 // Type or member is obsolete
}

The patch

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <mediaLibrary>
            <mediaProvider>
                <patch:attribute name="type">Client.Core.MediaProvider.NoSslPortMediaProvider, Client.Core</patch:attribute>
            </mediaProvider>
        </mediaLibrary>    
    </sitecore>
</configuration>
| improve this answer | |

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.