Developer forum

Forum » Development » Product URL slug generation algorithm

Product URL slug generation algorithm

Sigurd Nejrup
Reply

I have a case where the client occasionally edits product names slighty. This results in a new URL/slug for the product details page for the product.

The client would like to keep the original URL for SEO purposes.

 

To ensure the URL stays the same, I'm thinking of implementing a ProductBeforeSaveObserver which takes the product's name, generates the slug for it, and then applies that value on the product MetaUrl-field.

What algorithm does the product URL slug generation follow, so I can ensure it generates the correct slug?


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

The standard product URL slug in DW10 is generated from Product.Meta.Url if it has a value — otherwise from Product.Name (see ShopUrlDataProvider.cs).

That value is then passed through UrlHelper.SanitizeUrl(..., true) when the URL node is created.

How the slug is generated

In practice, the algorithm does roughly this:

  • Trim and lowercase
  • If LatinNormalize is enabled, apply mappings like:
    æ → ae, ø → oe, ö → oe, å → aa, ß → ss, ª → a, µ → u, º → o
  • Remove remaining diacritics
  • Replace whitespace and certain characters with -:
    • space, NBSP, ?, =, &, #, ,, ., \, +, ½
  • Replace / with - (because product URLs call SanitizeUrl(..., true))
  • Collapse repeated -
  • Keep only letters and digits
  • Trim leading/trailing -

If two full URLs collide, Dynamicweb automatically appends -1, -2, etc. (see UrlIndex.cs).

Recommended approach

If you want to match the built-in behavior, don’t reimplement the logic — just use the helper

 
using Dynamicweb.Frontend.UrlHandling;

if (string.IsNullOrWhiteSpace(product.Meta.Url))
{
product.Meta.Url = UrlHelper.SanitizeUrl(product.Name ?? string.Empty, true);
}
 

Important caveats

  • Don’t regenerate on every save
    Only set Meta.Url when it’s empty. Otherwise, changing the product name later will also change the slug.
  • URLs can still change
    Even with a fixed slug, the full product URL may change if the product’s group path changes (since group segments are part of the URL).
  • High volume mode
    If HighVolumeProductUrls is enabled, a different URL scheme is used (see ProductUrlQuerystringProvider.cs).
 
Sigurd Nejrup
Reply

Hi Nicolai

 

This is really helpful, thanks!

I forgot to mention that this is for a DW 9 solution. My apologies. It seems that UrlHelper.SanitizeUrl() is marked as internal in DW 9 but public in DW 10, so I cannot call it directly unfortunately.

I've since learned that if I put the product name directly inside the ProductMetaUrl field, it seems to generate the correct URL and as such would solve my problem. It’s also straightforward to implement, since product data is fetched from an ERP system - we can simply map the product name for new or updated products into this field during import.

It does feel a bit hacky though - do you think this approach could introduce complications in the future?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

This should work - we do not introduce significant changes to DW9, so if it works now it works tomorrow :-)

 

You must be logged in to post in the forum