1

we have the below configuration with Sitecore 9.3

<encodeNameReplacements>
<replace mode="on" find=" " replaceWith="-" />
</encodeNameReplacements>

When there is a media item with space in its name. e.g. "Test Media Item.pdf", the Url generates as www.test.com/-media/Test-Media-Item.pdf, when we use the item in RTE or Item link.

www.test.com/-media/Test-Media-Item.pdf - redirects to 404 page as item not found. If I try to access directly as www.test.com/-media/Test Media Item.pdf replacing "-" with "" able to browse the file directly.

This works fine for Sitecore items. e.g. for an item with name "Test Item". Url www.test.com/test-item works fine

"-" has to be present in the item name and we can not remove the encodeNameReplacements rule as we have too many deeplinks to the sitecore pages from other websites.

On a separate note, we were able to access the same item with Url www.test.com/-media/Test-Media-Item.pdf on Sitecore 8.2. But its redirecting to 404 after upgrading to Sitecore 9.3. Could not find any custom media handler that would made it work on Sitecore 8.2.

Have checked the below articles.

https://briancaos.wordpress.com/2017/12/08/sitecore-replace-spaces-with-dashes-in-urls/

https://community.sitecore.com/community?id=community_question&sys_id=3fc42fa11b4770d0b8954371b24bcbef

Can we handle this use case with a custom media handler may be ?

Please suggest.

1
  • Are you sure the issue is not when the document has a dash in the name to begin with? I just added two documents to vanilla 9.3 installation. "doc with space.pdf" and "doc-with-dash.pdf". The one with space workd every time (if i use space or dash). The one with dashes to being fails, because sitecore is trying to reverse the encodeNameReplacements logic and looks for "doc with dash.pdf" which doesnt exist. Commented Jun 13, 2023 at 14:16

1 Answer 1

0

I can give you some directions to start looking at the code of MediaRequest class. You may need to override Sitecore.Resources.Media.MediaRequest where you can update the logic to handle your media requests. You will get it in Sitecore.Kernel DLL.

And if you check the code of Sitecore.Resources.Media.MediaRequestHandler, it calls the MediaRequest class to get the URL and processes it like below.

protected virtual bool DoProcessRequest(HttpContext context)
{
  Assert.ArgumentNotNull((object) context, nameof (context));
  MediaRequest mediaRequest = this.GetMediaRequest(context.Request);
  if (mediaRequest == null)
    return false;
  string url = (string) null;
  Sitecore.Resources.Media.Media media1 = MediaManager.GetMedia(mediaRequest.MediaUri);
  if (media1 == null)
  {
    using (new SecurityDisabler())
      media1 = MediaManager.GetMedia(mediaRequest.MediaUri);
    if (media1 == null)
    {
      url = Settings.ItemNotFoundUrl;
    }
    else
    {
      Assert.IsNotNull((object) Context.Site, "site");
      if (!Context.User.IsAuthenticated && Context.Site.RequireLogin && !string.IsNullOrEmpty(Context.Site.LoginPage))
      {
        url = Context.Site.LoginPage;
        if (Settings.Authentication.SaveRawUrl)
        {
          UrlString urlString = new UrlString(url);
          urlString.Append("url", HttpUtility.UrlEncode(Context.RawUrl));
          url = urlString.GetUrl();
        }
      }
      else
        url = Settings.NoAccessUrl;
    }
  }
  else
  {
    bool flag = mediaRequest.Options.Thumbnail || media1.MediaData.HasContent;
    string lowerInvariant = media1.MediaData.MediaItem.InnerItem["path"].ToLowerInvariant();
    if (!flag && !string.IsNullOrEmpty(lowerInvariant))
    {
      Sitecore.Resources.Media.Media media2 = MediaManager.GetMedia(new MediaUri(lowerInvariant, Language.Current, Sitecore.Data.Version.Latest, Context.Database));
      if (media2 != null)
        media1 = media2;
    }
    else if (mediaRequest.Options.UseDefaultIcon && !flag)
      url = Themes.MapTheme(Settings.DefaultIcon).ToLowerInvariant();
    else if (!mediaRequest.Options.UseDefaultIcon && !flag)
      url = Settings.ItemNotFoundUrl;
  }
  if (string.IsNullOrEmpty(url))
    return this.DoProcessRequest(context, mediaRequest, media1);
  HttpContext.Current.Response.Redirect(url);
  return true;
} 

Finally, you can patch it like this.

<requestParser patch:instead="*[@type='Sitecore.Resources.Media.MediaRequest, Sitecore.Kernel']" type="YourClass, YourAssembly" />

Hope this will give you some directions to solve it.

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.