Sitecore Url Rewrite Module for Static File Extensions

I’d like to give a special thanks to my coworker Andy Cohen for writing the URL Rewrite module that I’m using for my current client. You can find the module here:

https://marketplace.sitecore.net/Modules/Url_Rewrite.aspx

The source code is located here:

https://github.com/iamandycohen/UrlRewrite

My clients old site had a bunch of PDF files that we needed to redirect to on their new Sitecore site. These PDFs were stored in the media library on the new Sitiecore instance. It was as simple as installing the URL rewrite module through the package manager and adding 1 line to the web.config:

<system.webServer>
    <handlers>
        <!-- Other Sitecore handlers -->
        <add verb="*" path="sitecore_media.ashx" type="Sitecore.Resources.Media.MediaRequestHandler, Sitecore.Kernel" name="Sitecore.MediaRequestHandler" />
        <!-- More Sitecore handlers -->  
        <!-- Line to add for the url rewrite module to process static files: -->
        <add name="UrlRewritePdfHandler" path="*.pdf" verb="*" type="Hi.UrlRewrite.Processing.UrlRewriteHandler" resourceType="Unspecified" preCondition="integratedMode" />
        <!-- Telerik handlers -->
    </handlers>
</system.webServer>

Next, I’m able to insert a redirect folder into my content tree and choose which site instance I want it to run under:

urlrewrite1

Note: A custom field type has been created to select a site that comes from the Sites section of your web.config. After I select a site, I am now able to insert a simple redirect:

urlrewrite2

On the “Target” field, I’ve clicked Insert Media Link to insert a PDF file. I’ve also set the “Path” field to be i1.pdf so any time a user navigates to <host name>/i1.pdf, they will be redirected to the appropriate media item in the media library.

How it works:

The Url Rewrite module will not work by default for paths that have static file extensions such as .pdf, .js or .css. The Hi.UrlRewrite.dll comes with a handler to take care of this, Hi.UrlRewrite.Processing.UrlRewriteHandler. By inserting this into the handlers section of our web.config, this code will be called instead of the static file handler. This code tries to find a rewrite and if one is found, will redirect to the appropriate URL. If no rewrite rule is found, it will then call the static file handler so the default functionality is not changed.

It’s important that this line comes after the Sitecore media handler so we do not change any of Sitecore’s default functionality. The line I’m talking about is called out above in the example config which contains the path of sitecore_media.ashx.

One thought on “Sitecore Url Rewrite Module for Static File Extensions

Add yours

  1. Is there any way I could use this URL Rewrite module to rewrite a URL that matches a pattern for Cache busting? Normally this is done by installation of the URL Rewrite Module in IIS and editing the web.config, but we are looking for an alternative.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: