Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Sanjay Kumar
Jun 21, 2024
  1351
(2 votes)

Remove a segment from the URL in CMS 12

Problem: I have created thousands of pages dynamically using schedule jobs with different templates (e.g. one column, two columns, etc..) and stored them under one of the specific container page templates but some of the page’s URLs were renamed.

So, I have two problems:

  1. How to ignore the container page name segment from the URL,
  2. Redirect to the new page without using any Add-ons or mapping the old one to the new URL.

Page Hierarchy

Name in URL

New URL (simple address url)

Expected result

Start Page -> Container Page -> One col page

study

students/study

- Remove the ‘container-page’ segment from URL.

- Apply 301 redirection for New URLs without adding any Add-Ons

https://local.alloy.com/container-page/study

 https://local.alloy.com/students/study

https://local.alloy.com/study

https://local.alloy.com/students/study

Solution: To fix the above problem we need to add the simple address for new URLs in each page and implement custom 301 redirections.

Register middleware into startup.cs file to bypass the container page segment from the URL

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configuration)
 {
      app.UseMiddleware<SkipContainerPageMiddleware>();
 }

Create the container page middleware to read the simple address and apply 301 redirection:

public class SkipContainerPageMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ISettingsService _settingsService;
    private readonly IContentLoader _contentLoader;

    public SkipContainerPageMiddleware(RequestDelegate next, ISettingsService settingsService, IContentLoader contentLoader)
    {
        _next = next;
        _settingsService = settingsService;
        _contentLoader = contentLoader;
    }

    public async Task Invoke(HttpContext context)
    {
       //Create a content reference type property in GlobalPageSettings or HomePage to read the container page location where all pages created
        var folderLocation = _settingsService.GetSiteSettings<GlobalPageSettings>()?.OEmbedFolderLocation ?? ContentReference.EmptyReference; 
        var folderName = "/" + _contentLoader.Get<IContent>(folderLocation).Name;

        if (context.Request.Path.StartsWithSegments(folderName, StringComparison.OrdinalIgnoreCase))
        {
            context.Request.Path = context.Request.Path.HasValue
                ? context?.Request?.Path.Value.Substring(folderName.Length)
                : string.Empty;

            if (context != null)
            {
                var url = GetSimpleAddress(folderName, context);
                if (url != null)
                {
                    var newURl = $"{context.Request.Scheme}://{context.Request.Host}/{url.Trim('/')}";
                    context.Response.Redirect(newURl, true);
                    return;
                }
            }
        }

        if (context != null)
            await _next(context);
    }

    private string? GetSimpleAddress(string parentSegment, HttpContext context)
    {
        var parentPage = _contentLoader
            .GetChildren<ContainerPage>(ContentReference.StartPage)
            .FirstOrDefault(x => string.Equals(parentSegment.Trim('/'), x.URLSegment, StringComparison.InvariantCultureIgnoreCase))?
            .ContentLink
            ?? ContentReference.EmptyReference;

        var url = context.Request.Path.HasValue ? context?.Request.Path.Value.Trim('/') : string.Empty;
        if (string.IsNullOrWhiteSpace(url) || parentPage == ContentReference.EmptyReference)
            return default;

        PageData? page = null;
        foreach (var item in url.Split('/'))
        {
            page = RecursivePageBySegment(parentPage, item);
            if (page != null)
                parentPage = page.ContentLink;
        }

        return page?.ExternalURL ?? url;
    }

    private PageData? RecursivePageBySegment(ContentReference parentPage, string urlSegment)
    {
        var children = _contentLoader.GetChildren<PageData>(parentPage);
        if (children.Count() == 0)
        {
            return _contentLoader.Get<PageData>(parentPage);
        }

        var page = children.FirstOrDefault(page => page.URLSegment.Equals(urlSegment, StringComparison.OrdinalIgnoreCase));
        return page ?? default;
    }
}

I hope this blog helps you achieve similar type functionality!

 

Jun 21, 2024

Comments

Please login to comment.
Latest blogs
A Free Course for Building Headless Applications with Next.js and Optimizely SaaS CMS

I am excited to announce the transformation of Optimizely Headless CMS webinar into a comprehensive, completely free self-paced course that's...

Szymon Uryga | Mar 24, 2025

Managed Identity for Connecting your Optimizely site to a Database in Azure

Are you using a connection string with username and password to connect to your Azure database? Use managed identity instead!

Tomas Hensrud Gulla | Mar 24, 2025 |

A day in the life of an Optimizely Developer - Creating a Cloudflare Turnstile Form Element Block

Hello and welcome to another installment of a day in the life of an Optimizely developer. Today I am going to show how to create a Cloudflare...

Graham Carr | Mar 24, 2025

What is ConstructorParameterResolver?

A few weeks ago I stumbled on a little-known feature and searching for it didn't reveal much about it. I managed to figure out how it's supposed to...

Daniel Ekeblad | Mar 21, 2025