SaaS CMS has officially launched! Learn more now.

Sanjay Kumar
Jun 21, 2024
  358
(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 Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024

GetNextSegment with empty Remaining causing fuzzes

Optimizely CMS offers you to create partial routers. This concept allows you display content differently depending on the routed content in the URL...

David Drouin-Prince | Jul 8, 2024 | Syndicated blog

Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024