Quan Mai
Mar 14, 2016
  4031
(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
Block type selection doesn't work

Imagine you're trying to create a new block in a specific content area. You click the "Create" link, expecting to see a CMS modal with a list of...

Damian Smutek | Nov 4, 2024 | Syndicated blog

.NET 8 FAQ

I have previously written about .NET compatibility in general and .NET 8 in particular, see blog posts here , here and here . With the end of suppo...

Magnus Rahl | Nov 4, 2024

Dynamic packages in Commerce Connect

In Optimizely Commerce Connect, you can group different items using packages and bundles. Package: A package has one or more versions of a product...

K Khan | Nov 1, 2024

Efficient Catalog Metadata Management and Product Updates Using DTOs in Optimizely Commerce

This post explores ways to manage and update catalog metadata in Optimizely Commerce by utilizing Data Transfer Objects (DTOs). DTOs provide a...

Sujit Senapati | Oct 31, 2024

Effortlessly Resize Images with Cloudflare's On-the-Fly Solution

Resizing images in C# has traditionally been a complex and time-consuming task, often requiring intricate code and handling various image processin...

Manoj Kumawat | Oct 31, 2024 | Syndicated blog

XSS Vulnerabilities Patched with TinyMCE 6.8.4

Two different XSS vulnerabilities were fixed in the latest update of the NuGet package EPiServer.CMS.TinyMce. Update today!

Tomas Hensrud Gulla | Oct 30, 2024 | Syndicated blog