Friday, 14 March 2025

Removing a Segment from the URL in Optimizely CMS 12 using Partial Routing

Problem Statement

In Optimizely CMS 12, dynamically generated pages inherit URL segments from their container pages. However, in certain cases, some pages are renamed, leading to undesired URL structures. The three primary challenges faced in this scenario are:

  1. Ignoring the container page name segment from the URL.

  2. Redirecting to the new page without using any add-ons or manually mapping old URLs to new ones.

  3. Create a URL for the 'View on Website' hyperlink without including container pages in the CMS.

Page Hierarchy and Expected Results

Consider the following page hierarchy:

  • Start PageContainer PageBlock Page

  • Start PageContainer Page 1Container Page 2Block Page

  • Expected Result:

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

      Start Page / Block Page

Example URLs

Original URL → Expected New URL

https://www.alloy.com/container-page/newshttps://www.alloy.com/news

https://www.alloy.com/container-page/news/article-1https://www.alloy.com/news/article-1

https://www.alloy.com/container-page1/container-page2/news/article-1https://www.alloy.com/news/article-1

https://www.alloy.com/container-page1/news/container-page2/article-1https://www.alloy.com/news/article-1

Solution Using Partial Router

To solve the problem, the following steps must be implemented:

  • Utilize Partial Routing to handle URL resolution and bypass container pages dynamically.

Using Partial Routing for URL Customization

Container pages, which inherit from PageData but do not have associated views or controllers, are frequently used to organize content in the tree. To provide flexibility, developers may introduce an optional boolean setting in the site settings page, allowing editors to enable or disable the visibility of the container page segment in URLs.

However, in this example, rather than using a configurable setting, we treat ContainerPage page type as container pages by default. This ensures that Partial Routing effectively resolves and cleans up URLs dynamically, omitting unnecessary container page segments.

Code Implementation

Below is the code snippet for handling these changes:

This approach ensures a cleaner URL structure and avoids unnecessary dependencies while maintaining flexibility in content organization.

About the Author

Adnan Zameer, Lead Developer at Optimizley UK, is a certified Microsoft professional, specializing in web app architecture. His expertise includes Optimizley CMS and Azure, showcasing proficiency in crafting robust and efficient solutions.

0 comments :

Post a Comment