Redirect your product URL:s in Episerver Commerce

Why redirect?

A very good thing about Episerver Commerce is that even you enabled the partial routing system, the old SEO Uri:s will continue to work (of course, as long as you keep the Uri:s unchanged). So they can coexist in same website – but that might not what you want – you might want to stick with only one routing system – it’s been told that the more popular your URL is, the higher rank it gets in the search engine results.

From hierarchial URL to SEO URL:

This one is pretty easy, just call this during your site’s initialization, the true parameter means you want to enable Outgoing Seo URI, and the next statement configures the redirect:

CatalogRouteHelper.MapDefaultHierarchialRouter(RouteTable.Routes, true);

CatalogRouteHelper.SetupSeoUriPermanentRedirect();

From SEO URI to hierarchial URI:

This one is tricky, there is no such built-in method allows us to do so. We can’t even override SeoUriRouter because it’s registered automatically and there is no way to guarantee that our router will be able to run before it. (If a matching router is found, the processing will stop).

Let’s explore other options. EPiServer.Web.Routing.ContentRoute has two events which might be interesting – RoutingContent and RoutedContent. As their names might suggest, the former is fired before any routers take place, while the latter is fired after that. We might not want to listen to RoutingContent because it’s too early, we’ll have to process all URI:s sent to our site, which will slow it down. It’s better to only process after the URI has been routed successfully, preferly by SeoUriRouter. We can do some checks and redirect if necessary.

First, we need to register to RoutedContent event:

ContentRoute.RoutedContent += Routed_SeoUri;

And then implement it. It’s another void method which takes an object as sender and an RoutingEventArgs as the EventArgs.

RoutingEventArgs has SegmentContext property named RoutingSegmentContext, this is what we need.

A successfully routed route will set the RouteObject of RoutingSegmentContext to the content it found. So we can check if that content is a catalog content or not, to see if we should redirect.

But how can we know if the URI was SEO URI, or hierarchial URI? By checking for slash in the between? Not really effective, because the top level content only has one segment. We can mimick SeoUriRouter and try to load the Entry/Node by Uri? Yes that might work but it is not really fast. If we are lucky and the URI was SEO URI, the DTO should have been cached, but if it was hierarchial URI, we will have to hit the database to find nothing!

Solution, well, HierarchicalCatalogPartialRouter will process to the last segment, and for each segment, the LastConsumedFragment is assigned to next segment, so it’ll be empty at the end. SeoUriRouter, in other hand, does not really process any segment, so the LastConsumedFragment it returns will be the same path as requested.

With that information, we can have this as our method:

private static void Routed_SeoUri(object sender, RoutingEventArgs e)
{
    var context = e.RoutingSegmentContext;
    //RoutedObject is supposed to not be null here
    if (!(context.RoutedObject is CatalogContentBase))
    {
        return;
    }

    if(string.IsNullOrEmpty(context.LastConsumedFragment))
    {
         return;
    }
    var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
    context.PermanentRedirect(urlResolver.GetUrl(context.RoutedContentLink));
}

And now we can redirect the SEO URI to Hierarchical URI in an effective way.

Shameless plug: This is an excerpt from the book I’m writing, Pro Episerver Commerce, which can be purchased here.

One thought on “Redirect your product URL:s in Episerver Commerce

Leave a Reply

Your email address will not be published. Required fields are marked *