UrlRewriteModule

You are currently browsing articles tagged UrlRewriteModule.

Google and other search engines does not like duplicate content. The reason is that the page’s reputation (or page rank) is calculated from incoming internal and external links. If you have more links leading to the same page the reputation will be split between the URLs.

One an EPiServer site with English as default language in siteHosts-tag the start page can usually be reached by the following two URLs. (Read more about siteHosts, Globalization and how to set the default language for a domain.)

  1. www.mydomain.com/
  2. www.mydomain.com/en/

EPiServer’s Friendly URL Rewriter adds “/en” to the beginning to select Language Branch if the internal URL contains the query parameter epslanguage.  So you can have a lot of both URLs on your site, both leading to the same page.

One recommended strategy to handle multiple URLs to the same page is to use 301 Redirection but that is a topic for another blog post (Ted Nyberg had a good post about this some time ago). In this post I want to show you how easy it is to tweak EPiServer’s Friendly URL Rewriting to eliminate duplicate URLs caused by language.

Example of how to tweak EPiServer’s Friendly Url Rewriter

public class Global : EPiServer.Global
{
    protected void Application_Start(Object sender, EventArgs e)
    {
        EPiServer.Web.UrlRewriteModule.HttpRewriteInit += UrlRewriteModule_HttpRewriteInit;
    }

    /// <summary>
    /// This adds the hooks to the UrlRewriter instance
    /// </summary>
    private void UrlRewriteModule_HttpRewriteInit(object sender, UrlRewriteEventArgs e)
    {
        UrlRewriteModuleBase module = sender as UrlRewriteModuleBase;
        if (module != null)
        {
            module.HtmlAddingRewriteToExternalFilter += module_HtmlAddingRewriteToExternalFilter;
            UrlRewriteProvider.ConvertedToExternal += UrlRewriteProvider_ConvertedToExternal;
        }
    }

    /// <summary> 

    /// Remove language from path if it is the default for the hostname.

    /// </summary> 

    private void UrlRewriteProvider_ConvertedToExternal(object sender, UrlRewriteEventArgs e)
    {
        if (e.IsModified)
        {
            string lang = LanguageSelection.GetLanguageFromHost();
            if (e.Url.Path.StartsWith( '/' + lang + '/'))
            {
                e.Url.Path = e.Url.Path.Substring(lang.Length + 1);
            }
        }
    }

    /// <summary>
    /// Prevent Rewrite To External for Trace.axd and 

    /// other locations I do not want EPiServer to interfere with.
    /// </summary>
    private void module_HtmlAddingRewriteToExternalFilter(object sender, UrlRewriteEventArgs e)
    {
        string path = e.Url.Path.ToLowerInvariant();
        e.Cancel = e.Cancel || path.EndsWith("trace.axd") || path.StartsWith("/MyWebApp")

    }
}

Tags: , , , , ,

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.

Tags: , , , ,