Take the community feedback survey now.

Quan Mai
Mar 14, 2016
  4398
(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
A day in the life of an Optimizely OMVP - AEO & GEO: The Future of Digital Visibility with Optimizely

The way people discover content online is undergoing a seismic shift. Traditional SEO is no longer enough. With AI-powered tools like ChatGPT,...

Graham Carr | Sep 12, 2025

Building Optimizely OCP Apps Faster with AI and Coding Assistants

Developing Optimizely Connect Platform (OCP) apps can be a rewarding but complex process—especially when integrating with external APIs. Over the...

Pawel Zieba | Sep 11, 2025

New Opal Certifications Are Live and Free!

We’ve got some exciting news to share: two brand-new Opal certifications are now available and they’re completely free. Whether you’re already...

Satata Satez | Sep 10, 2025

Going Headless: On-Page Editing with Optimizely Graph and Next.js

Introduction On-page editing is one of the standout features of Optimizely CMS, giving editors the power to update content directly on the site as...

Michał Mitas | Sep 10, 2025

Dynamic CSP Management for Headless and Hybrid Optimizely CMS with Next.js

In the evolving realm of web security, Content Security Policy (CSP) is essential for defending against XSS and injection attacks. Traditional...

Minesh Shah (Netcel) | Sep 8, 2025

Optimizely : Fast Installation on Local Machine

I have been exploring Optimizely and installed CMS 12 on my local machine pretty quickly using the NuGet package manager. Assuming all prerequisite...

Madhu | Sep 8, 2025 |