Egli Becerra
Egli Becerra

Reputation: 1040

Sitecore 8.2 - URL Rewrite Module does not process items from Sitecore Media Library

I've been dealing with this for a while now and given up, and decided to see if anyone else had a similar problem before.

So, I have the URL rewrite module from Andy Cohen (using Sitecore 8.2) https://github.com/iamandycohen/UrlRewrite

In general the redirects are working fine, even for static files such as .pdf (I have added the extra configuration in web.config to read *.pdf files etc) referenced in (https://horizontal.blog/2014/12/19/sitecore-url-rewrite-module-for-static-file-extensions/)

The problem comes when trying to redirect a .pdf from the media library. If an item inside the media library is called then this event won't trigger the InboundRewriteProcessor.cs where the request kicks off the pipeline.

My media library items follow the below structure:

https://<base_url>/-/media/<site folder>/files/<filename>.pdf

A very good blog regarding this is Matt's Sitecore Art blog

https://sitecoreart.martinrayenglish.com/2016/12/implementing-3000-redirects-in-sitecore.html

Any tips on how to get the Sitecore UrlRewrite module to redirect items inside the media library when the media url starts with -/media/ would be greatly appreciated.

Upvotes: 1

Views: 618

Answers (1)

Kate Orlova
Kate Orlova

Reputation: 3283

By design, Sitecore maps both -/media and ~/media directly to the media handler, therefore, requests with ~/media prefix never reach the redirect logic implemented by InboundRewriteProcessor.cs. This is from Sitecore.config:

<customHandlers> 
 <handler trigger="-/media/" handler="sitecore_media.ashx" />
 <handler trigger="~/media/" handler="sitecore_media.ashx" />
 ...
</customHandlers> 

We have encountered a similar problem as yours multiple times and found a long-term solution through implementing a custom media handler to support the desired redirect behaviour.

Here is a code example:

public class CustomMediaRequestHandler : MediaRequestHandler
{
  protected override bool DoProcessRequest(HttpContext context)
  {
    Assert.ArgumentNotNull((object)context, "context");
    MediaRequest request = MediaManager.ParseMediaRequest(context.Request);
    if (request == null) return false;
    Media media = MediaManager.GetMedia(request.MediaUri);
    if (media != null) return base.DoProcessRequest(context);
    if (media == null)
    {
      using (new SecurityDisabler()) media = MediaManager.GetMedia(request.MediaUri);
      if (media == null)
      {
        //TODO: call your custom redirect method
      }
    }
    return base.DoProcessRequest(context);            
  }

  public void RedirectMediaRequest(string requestedUrl, HttpRequestArgs args = null)
  {
     //TODO: implement your custom redirect logic
  }
}

Once the custom redirect handler is implemented, remember to update the web.config file to link Sitecore.MediaRequestHandler to it explicitly as follows:

<system.webServer>
  <handlers>       
    <add verb="*" path="sitecore_media.ashx" type="YourNamespace.CustomMediaRequestHandler, YourAssembly" name="Sitecore.MediaRequestHandler" />
  </handlers>
</system.webServer>

Upvotes: 3

Related Questions