Minesh Shah (Netcel)
Feb 28, 2023
  2387
(5 votes)

URL Rewrites in CMS12 (.Net 6)

URL rewriting is a common technique used in web applications to create user-friendly URLs that are easier to understand, provide consistency and have SEO benefits. In the past, URL rewriting was commonly accomplished using IIS URL Rewrite module. However, with the release of .NET Core, the process of rewriting URLs has undergone some changes.

Microsoft has introduced a new URL rewriting middleware called Microsoft.AspNetCore.Rewrite. This middleware is part of the ASP.NET Core framework and is designed to rewrite URLs in a much more flexible and efficient way.

One of the key benefits of using the Microsoft.AspNetCore.Rewrite middleware is that it allows URL rewriting without requiring IIS or any other web server. This means that we can create Optimizely Solutions that are completely self-contained and can be run on any platform.

Here is an example of how to use the Microsoft.AspNetCore.Rewrite middleware in .NET 6 to handle some common conventions like redirecting to https, enforcing lowercase urls and adding trailing slash to the end of all URLs:

Lower Case URL – Implement IRule Base Rule

    public class LowercaseUrlsRule : IRule
    {
        public int StatusCode { get; } = (int)HttpStatusCode.MovedPermanently;

        public void ApplyRule(RewriteContext context)
        {
            HttpRequest request = context.HttpContext.Request;
            PathString path = context.HttpContext.Request.Path;
            HostString host = context.HttpContext.Request.Host;

            if (path.HasValue && path.Value.Any(char.IsUpper) || host.HasValue && host.Value.Any(char.IsUpper))
            {
                HttpResponse response = context.HttpContext.Response;
                response.StatusCode = StatusCode;
                response.Headers[HeaderNames.Location] = (request.Scheme + "://" + host.Value + request.PathBase.Value + request.Path.Value).ToLower() + request.QueryString;
                context.Result = RuleResult.EndResponse;
            }
            else
            {
                context.Result = RuleResult.ContinueRules;
            }
        }
    }

Use Rules in Middleware Startup.cs

In the example below we are using the RewriteOptions class to define paths that should be negated, and adding the rules for forcing HTTPS, Lowercase and Trailing Slashes. I have explicitly added below app.UseStaticFiles() so the rules do not get added to files like css, javascript or images.

        app.UseStaticFiles();

        var options = new RewriteOptions()
            .Add(context =>
                {
                    if (context.HttpContext.Request.Path.StartsWithSegments("/util") || 
                        context.HttpContext.Request.Path.StartsWithSegments("/episerver") || 
                        context.HttpContext.Request.Path.StartsWithSegments("/modules"))
                    {
                        context.Result = RuleResult.SkipRemainingRules;
                    }
                })
            // Redirect to HTTPS
            .AddRedirectToHttpsPermanent()
            // Enforce lower case. 
            .Add(new LowercaseUrlsRule())
            // Enforce trailing slash.
            .AddRedirect("(.*[^/])$", "$1/");



        app.UseRewriter(options);

These techniques are not Optimizely specific and can be applied to any .Net Solution, more info on IRule based rewrites can be found here

Feb 28, 2023

Comments

EVT
EVT Jul 25, 2023 10:37 AM

Row

response.Headers[HeaderNames.Location] = url.ToLowerInvariant() + request.QueryString;

fails if there is a non ascii character in the URL. Perhaps it should be changed to 

response.Headers[HeaderNames.Location] = UriHelper.Encode(new Uri((request.Scheme + "://" + host.Value + request.PathBase.Value + request.Path.Value).ToLower() + request.QueryString));

Tim Hilton
Tim Hilton Jan 12, 2024 05:19 PM

Thanks for a useful article Minesh.

Just a point to note, these redirects are processing sequentially causing a number of redirects if mutiple conditions are met. 

For example if the url has uppercase characters and a missing trailling slash the response is 301 lowercase > 302 trailling slash > 200 ok.

A minor update to the LowercaseUrlsRule, to resolve the other conditions at the same time helps this.

string slash = !path.Value.EndsWith("/") ? "/" : string.Empty;
string lowerPath = $"https://{host.Value}{request.PathBase.Value}{request.Path.Value}{slash}".ToLower();
...

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024