Take the community feedback survey now.

Quan Mai
Mar 14, 2016
  4472
(3 votes)

Redirecting your product URL:s

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 my working book, Pro Episerver Commerce, which can be purchased here.

Mar 14, 2016

Comments

Please login to comment.
Latest blogs
Optimizely CMS - Learning by Doing: EP06 - Create Header, Footer, Menu & Component/View for Blocks

  Episode 6  is Live!! The latest installment of my  Learning by Doing: Build Series  on  Optimizely CMS 12  is now available on YouTube! This vide...

Ratish | Nov 4, 2025 |

Going Headless: 3 Ways to Store Custom Data in Optimizely Graph

Welcome to another installment of my  Going Headless  series. Previously, we covered: Going Headless: Making the Right Architectural Choices Going...

Michał Mitas | Nov 3, 2025

A day in the life of an Optimizely OMVP - What's New in Optimizely CMS: A Comprehensive Recap of 2025 Updates

Hello and welcome to another instalment of a day in the life of an Optimizely OMVP. On the back of the presentation I gave in the October 2025 happ...

Graham Carr | Nov 3, 2025

Optimizely CMS Mixed Auth - Okta + ASP.NET Identity

Configuring mixed authentication and authorization in Optimizely CMS using Okta and ASP.NET Identity.

Damian Smutek | Oct 27, 2025 |