Try our conversational search powered by Generative AI!

Quan Mai
Mar 14, 2016
  3889
(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 Developer - Enabling Opti ID within your application

Hello and welcome to another instalment of A Day In The Life Of An Optimizely developer, in this blog post I will provide details on Optimizely's...

Graham Carr | May 9, 2024

How to add a custom property in Optimizely Graph

In the Optimizely CMS content can be synchronized to the Optimizely Graph service for it then to be exposed by the GraphQL API. In some cases, you...

Ynze | May 9, 2024 | Syndicated blog

New Security Improvement released for Optimizely CMS 11

A new security improvement has been released for Optimizely CMS 11. You should update now!

Tomas Hensrud Gulla | May 7, 2024 | Syndicated blog

Azure AI Language – Key Phrase Extraction in Optimizely CMS

In this article, I demonstrate how the key phrase extraction feature, offered by the Azure AI Language service, can be used to generate a list of k...

Anil Patel | May 7, 2024 | Syndicated blog