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:
The source code is located here:
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:
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:
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.