Disable EPiServer UrlRewriter Interference

Are you also annoyed that EPiServer CMS 5 Friendly URLs and the handy trace.axd utility does not play well together? Does your site have standard aspx-pages that behaves strangely sometimes because EPiServer Friendly URL Rewriter interfere with generated html on the way out?

StarCommunity does not work EPiServer Friendly URL Rewriter

I had this issue with StarCommunity 2.4 from NetStar (now bought by EPiServer) and you could get the most strange side effects in the admin interface with tabs not working, layout issues and navigational problems. All issues disappear if you disabled EPiServer Url Rewriting.

So how can you keep EPiServer Url Rewriting on but disable it for a folder or file? This is how I solved it by adding a few lines in Global.asax.cs:

private void UrlRewriteModule_HttpRewriteInit(objectsender, UrlRewriteEventArgs e)
{
    UrlRewriteModuleBase module = sender asUrlRewriteModuleBase;
    if (module != null)
    {
        module.HtmlAddingRewriteToExternalFilter += module_HtmlAddingRewriteToExternalFilter;
    }
}
private void module_HtmlAddingRewriteToExternalFilter(objectsender, UrlRewriteEventArgs e)
{
    string path = e.Url.Path.ToLowerInvariant();
    e.Cancel = path.StartsWith("/netstar") || path.EndsWith("trace.axd");
}
protected void Application_Start(Object sender, EventArgs e)
{
    EPiServer.Web.UrlRewriteModule.HttpRewriteInit += UrlRewriteModule_HttpRewriteInit;
}

As you can see when the application starts it hooks into an event that hooks into another event that has the possibility to prevent Rewrite. Here we check if the requested URL is either starting with “/netstar” disabling rewrite for everything in the StarCommunity admin folder. It also cancels if it is a call to the trace utility.