Deane Barker
May 14, 2010
  11676
(4 votes)

Redirecting to a Simple Address

I was doing an EPiServer demo the other day, and was the showing the “simple address” feature that lets you map pages to simple URLs like “/register” or “/info.”

The client asked if those simple URLs could redirect rather than rewrite.  At the default, if you map a page to a simple address, it’s available both at that URL and its normal URL further down in the tree.  This could be problematic for some organizations to have duplicate content at two URLs.

Just for fun, I set out to solve the problem, and managed to bang it out in a dozen lines of code (most of it error checking).

The timing of where the code runs is a little tricky.  I thought about writing an HttpModule for it, but I needed to have the EPiServer page in hand when the code ran.  So, I need to run this code before anything on the page runs, but after the EPiServer content is mapped to the incoming request.  And it obviously need to run on every page, so I was looking for a way to catch the first moment we’re sure the visitor wants an EPiServer content object.

The method I used was to write a PagePlugin that hooks into the Init event of the page, like this:

public static void Initialize(int optionFlag)
{
    PageBase.PageSetup += new PageSetupEventHandler(PageSetup);
}

public static void PageSetup(PageBase sender, PageSetupEventArgs e)
{
    sender.Init += new EventHandler(CheckForSimpleAddressAccess);
}

What we’ve done with this is hooked an event very early into the page lifecycle.  This runs before anything in the actual page code-behind, so we can essentially pre-empt the entire page execution.

After checking that we’re on a TemplatePage with a valid (non-null) EPiServer page, I check to see if the RawUrl is the same as the simple address for the page.  If it is, I assume they came in on the simple address and redirect them to the real address.  The code for this is pretty simple:

if (String.Concat("/", (sender as PageBase).CurrentPage.Property["PageExternalURL"].ToString()) == HttpContext.Current.Request.RawUrl)
{
    Http.Current.Response.Redirect((sender as PageBase).CurrentPage.StaticLinkURL, true);
}

I took it one step further and added the ability to have a “Redirect to Simple Address” checkbox, so you can select whether a simple address will act as a rewrite or a redirect on a page-by-page basis.  If that doesn’t interest you, it should be pretty simple to comment those lines out and just have this code execute in all cases.

Just a quick warning: this code tested fine, but has not been implemented in any actual environment (I was just solving a problem for fun, remember), so should not be considered production-ready.  If you find any bugs, drop me an email.

Here’s the download.  The zip contains a single class file.  Compile it into your project.

May 14, 2010

Comments

Sep 21, 2010 10:33 AM

Haven't tested it yet but it looks like nice work. :)

Ben Nitti
Ben Nitti Jun 24, 2020 09:41 PM

This would be great.  Ths download link is returning 404. Do you still have this code available?

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