0

We are using Sitecore 10.3 and we have integrated Azure CDN.

But now the situation is that we have multi site and for each site we have to use "Media.MediaLinkServerUrl" according to site domain.

Like

site1.com will be use https://site1cdnendpoint.azureedge.net
site2.com will be use https://site2cdnendpoint.azureedge.net
site3.com will be use https://site3cdnendpoint.azureedge.net

enter image description here

2
  • What have you tried? What exactly is your question, its not clear.
    – Richard Seal
    Commented Apr 10 at 14:01
  • We have enabled cdn.config. "Media.MediaLinkServerUrl" is common to all sites. But the requirement is that We must have to use site specific azure cdn url as mentioned above. Not getting idea how to configure site specific cdn Commented Apr 10 at 17:14

1 Answer 1

0

You will need to override the Media Provider class to generate different host names for media url, based on context site.

In order to achieve that, media provider needs to be overridden with custom class using below configuration and Media.AlwaysIncludeServerUrl configuration set to true.

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore role:require="ContentDelivery">
      <mediaLibrary>
        <mediaProvider>
          <patch:attribute name="type">SitecoreExtension.CustomMediaProvider, 
         SitecoreExtension</patch:attribute>
         </mediaProvider>
      </mediaLibrary>
      <settings>
        <setting name="Media.AlwaysIncludeServerUrl">
          <patch:attribute name="value">true</patch:attribute>
        </setting>
      </settings>
  </sitecore>
</configuration>
  

Thereafter you will need to override the GetMediaUrl function inside CustomMediaProvider as below:

using Sitecore.Resources.Media;
using Sitecore.Links.UrlBuilders;
using Sitecore.Data.Items;
using System;

public class CustomMediaProvider : MediaProvider
{
    public override string GetMediaUrl(MediaItem item, MediaUrlBuilderOptions options)
    {
        var mediaUrl = base.GetMediaUrl(item, options);
        if (!string.IsNullOrEmpty(mediaUrl) && options.AlwaysIncludeServerUrl.HasValue && options.AlwaysIncludeServerUrl.Value)
        {
            var mediaPathAndQueryUrl = GetPathAndQuery(mediaUrl);
            var cdnPath = Sitecore.Context.Site.Properties["CDNPath"];
            if (!string.IsNullOrEmpty(cdnPath))
            {
                return string.Format("{0}{1}", cdnPath, mediaPathAndQueryUrl);
            }
            else
            {
                return mediaPathAndQueryUrl;
            }
        }
        return mediaUrl;
    }

    private string GetPathAndQuery(string url)
    {
        Uri originalUrl;
        if (Uri.TryCreate(url, UriKind.Absolute, out originalUrl))
        {
            return originalUrl.PathAndQuery;
        }
        return url;
    }
}

This will generate different host names for each media depending on Site Context.

Above code reads the cdn path for each site from the site attribute "CDNPath". Therefore you can set this attribute under each site configuration as below.

enter image description here

Note: Above patch is only applicable on CD environments through adding role:define as ContentDelivery in the above configuration.

Hope this helps!!!

8
  • Hi @ckhanna, Thank you so much for the solution. I will check Commented Apr 12 at 10:18
  • sure @ManishDalwadi...you can check and let me know in case you face any trouble implementing this..
    – ckhanna
    Commented Apr 12 at 11:22
  • Hi @ckhanna, Meta tags image url is not working as expected. Meta images loads domain url two times. first url is site url and next one is specific site cdn url. <meta property="og:image" content="https://siteone-cm.qalocal.comhttps://testcdnpath.azureedge.net/-/media/Project/TestTenant/SiteOne/Common/Social-image.png?rev=876ca50c43144700b6658dd628313a2f"> Commented Apr 16 at 13:33
  • Hi @ManishDalwadi I have updated above code to take consideration for AlwaysIncludeServerUrl flag in media url options. This will prevent from the above issue to occur. Also make sure Media.AlwaysIncludeServerUrl is set to true as patched in above configuration. Let me know in case you still face any issue.
    – ckhanna
    Commented Apr 16 at 16:00
  • Hi @ckhanna, Thanks for the quick response. One more issue i am facing is Styles and script urls are still using default cdn domain instead site sepcific domain. Am i doing wrong or missing anything? Commented Apr 16 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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