Trailing slashes in resolved urls

2025/02/18 2:42 PM

Is there a recommended approach for ensuring trailing slashes are used throughout the website?

In the documentation it talks about the default being:

Retrieved URLs are always returned in lower case without a trailing slash.

Is there a way of configuring to make sure XbyK always renders URLs with a trailing slash?

The documentation mentions some options can be configured through WebPageUrlRetrieverOptions and UrlResolveOptions but they don't seem to have options for trailing slash. Is this just not added yet?

My ideal scenario here would be a configuration option to ensure URLs retrieved through the retriever, and URLs rendered in Rich Text would be handled correctly.

Answers

2025/02/18 5:03 PM

Not a great answer, but if Kentico can open up the IWebPageUrlFormatter to be overwritten (currently internal), OR instead of using the WebPageUrlPathUtils.FormatUrlPath(urlPath) (static method), replace it with an interface to be customized, that would work...

As it stands, i don't see a way either :-(

private static string FormatUrlPath(string urlPath)
{
    return "/" + WebPageUrlPathUtils.FormatUrlPath(urlPath);
}


...

public static class WebPageUrlPathUtils
    {
        /// <summary>
        /// Formats web page URL path.
        /// </summary>
        /// <param name="urlPath">URL path to be formatted.</param>
        /// <remarks>Formatted URL path is lowercased and without trailing slash.</remarks>
        public static string FormatUrlPath(string urlPath)
        {
            if (string.IsNullOrEmpty(urlPath))
            {
                return string.Empty;
            }

            return urlPath.ToLowerInvariant().TrimEnd('/');
        }
    }
2025/02/18 7:41 PM

I don't believe there is a way to customize this today. Is the trailing slash required for something to work correctly? I think consistency is more important for SEO and redirects, right? The internet doesn't generally care whether URLs end in / or not.

That said, I've taken a similar approach to Trevor, creating extension methods on WebPageUrl for the Kentico Community Portal, except I remove the ~ prefix of generated URLs because those don't work outside of ASP.NET Core rendering and URL resolving.

2025/02/19 9:28 AM

Yeah consistency is key, but giving the flexibility of which way to enforce is important.

Clients (and their SEO teams) often demand that URLs are output in HTML in a particular way - sometimes they demand with and sometimes without a trailing slash. So the current XbyK default works fine for the latter, but makes it difficult for the former.

We already have a middleware to handle the redirection to the correct version, and I think we've looked at doing something similar to what you've both mentioned in terms of creating an extension, but ideally there would be a configuration where XbyK would respect this as a configuration option.

Thanks for the suggestions - I'll also submit to the roadmap as an idea!

To answer this question, you have to login first.