Quantcast
Channel: briancaos – Brian Pedersen's Sitecore and .NET Blog
Viewing all articles
Browse latest Browse all 276

URL Rewrite and Sitecore

$
0
0

The Microsoft URL Rewrite module is an ISS extension that allows you to rewrite one URL to another using regular expressions. The extension is like the Sitecore aliases on steroids, and is especially useful for mapping old dynamic URL’s to new Sitecore URL’s.
If you launch a new website on an existing domain, you can be forced to keep old API URL’s as they were, and the URL Rewrite will help you with that.

Download the URL Rewrite extension here.

Here is an example on how to use URL Rewrite Rules to map an old API URL to a new Sitecore .ashx CustomHandler.

The URL rewrite rules are easiest kept in a separate configuration file, so you need to apply the following to the system.webserver part of your web.config:

<system.webServer>    
  ...
  <rewrite>
    <rules configSource="App_Config\UrlRewriteRules.config" />
  </rewrite>
</system.webServer>

You then place the rewrite rules in a file called UrlRewriteRules.config  in the App_Config folder:

<?xml version="1.0" encoding="utf-8"?>
<rules>
  <clear />
  <rule name="ImageScaler" stopProcessing="true">
    <match url="^/scaleimage/([^/]*)/w/(\d+)/h/(\d+)/name/([a-zA-Z]+)\.([a-zA-Z]+)$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
    <action type="Rewrite" url="/Sitecore Modules/web/ImageResizer.ashx?id={R:1}&amp;width={R:2}&amp;height={R:3}&amp;name={R:4}&amp;extension={R:5}" appendQueryString="false" logRewrittenUrl="true" />
  </rule>
  ...
  ...
</rules>

The “match” regular expression determines which URL to be redirected.
In this example I try to catch /scaleimage/1200/w/800/h/600/name/scaledimage.jpg.
The “rewrite” determines the URL to rewrite to (in this case /Sitecore Modules/web/ImageResizer.ashx) and the regular expression matches are appended as parameters to the .ashx page by the {R:n} syntax.

This was the URL Rewrite part, now onto some code.

I need to register my /Sitecore Modules/web/ImageResizer.ashx in Sitecore, and I can do this by adding the following to the web.config:

</sitecore>
  <customHandlers>
    ...
    <handler trigger="/Sitecore Modules/web/ImageResizer/" handler="ImageResizer" />
  </customHandlers>
</sitecore>
...
...
<system.webserver>
  <handlers>
    ...
    <add verb="*" path="/Sitecore Modules/web/ImageResizer.ashx" type="MyCode.ImageResizer, MyCode" name="ImageResizer" />
  </handlers>
</system.webserver>

The .ashx handler is ready to be called (example code only, will not compile):

namespace MyCode
{
  public class ImageResizer : IHttpHandler
  {
    public void ProcessRequest(HttpContext context)
    {
      // Retrieving the parameters from the request as defined in the 
      // "rewrite" rule:
      // ?id={R:1}&amp;width={R:2}&amp;height={R:3}&amp;name={R:4}&amp;extension={R:5}
      string ID = context.Request.QueryString["id"];
      string width = context.Request.QueryString["width"];
      string height = context.Request.QueryString["height"];
      string name = context.Request.QueryString["name"];
      string extension = context.Request.QueryString["extension"];

      // Do some code to get the image and resizing it
      Image image = GetSomeImaginaryImageClass();

      // Stream it back to the user
      context.Response.Clear();
      context.Response.ContentType = "image/jpeg";
      context.Response.StatusCode = (int)HttpStatusCode.OK;
      context.Response.BufferOutput = true; 
      byte[] bytes = image.GetBytes();
      context.Response.OutputStream.Write(bytes, 0, bytes.Length);
      context.Response.Flush();
    }
  }
}

MORE TO READ:

 



Viewing all articles
Browse latest Browse all 276

Trending Articles