November Happy Hour will be moved to Thursday December 5th.

Sanjay Kumar
Jun 21, 2024
  950
(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
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog