Short URL redirects in Expression Engine, without add-ons

Today I had the need to create custom short URLs for a client. They wanted a single keyword as the short URL. I first looked into the Shrimp plugin, and then the Redirect301 plugin, but both relied on creating a new template group with a single character name, such as "u", which ends up with URLs like mysite.com/u/123. While this was close to what I needed to achieve, it wasn't a viable solution because of an unnecessary extra URI segment.

After a little creative thinking, and taking advantage of Expression Engine's flexibility I came up with a solution... Create a new template group called Redirects, with the short name redirects. Turn off Strict 404s, and create 2 new templates, one to be the actual 404 page you want to serve, and the other being the interstitial one that will be assigned as the 404 page in Expression Engine, in my example I chose 404_catch. What this will do is any time Expression Engine can't find a requested page, it will load the 404_catch template, which includes the following template code that first searches the redirects Weblog for any entries with a slug value the same as the first URI segment. If none is found, then it will then redirect to your actual 404 page, but if one is found, it will redirect to the URI you defined in the entry. It may be a good idea to disable most of the tabs in the Publish page, just because they're unnecessary, and to disable options such as field formatting, and even the URL Title field. I chose to prefix every entry in the Redirects weblog with redirect-.

I believe another way to accomplish is to use the Pages module, and create a single segment page for an entry, but since I'm already using Structure to manage my pages, this wasn't an option. I also have about 10 different content types, and a couple of URI patterns used on the site, so I needed a solution that wouldn't interfere with Structure, and be flexible enough to work with any URI pattern. Unfortunately there is no way to prevent duplicate entries of the same URL slug, so that is a big caveat, but in my client's case, they won't have hundreds or thousands of these short URLs. If a duplicate comes up, it should be easy to notice the issue and change the URL slug.

site/404_catch template{exp:weblog:entries weblog="redirects" disable="pagination|categories|member_data" limit="1" search:redirect_slug="{segment_1}"} {if no_results} {redirect="site/404"} {/if} {redirect="{redirect_url}"} {/exp:weblog:entries}

Disable all unnecessary Publish form options.

Create a Redirect.

Setup the 404 page you want to use.

Posted on: Tuesday, September 29, 2009

Posted by: Used Engines on 04/28 at 03:11 PM

I love this stuff!

-jman
webmaster at
Used Engines

Posted by: teeth whinening on 06/06 at 07:43 AM

Very interesting, i find this on twitter and now i retwitted this, too.

Posted by: payday loans on 06/06 at 07:43 AM

Thanks for taking the time to post such a detailed and informative article.

Posted by: best acne treatment on 07/03 at 12:04 AM

Well I really like the ad and i think there is an immediate need of this among youngsters, It’s a fastidious and instructive. Things are ordered wellspring.

Posted by: Acai Berry on 07/05 at 03:20 AM

Thanks for the nice post. I am expecting some different idea from your side. You always represent some new thought in your post.

Posted by: ElMothaeda Web on 07/22 at 04:40 AM

Good article .. Thank you

Posted by: Instant Social Anarchy on 08/04 at 05:17 AM

Brilliant post Buddy. I am using Expression Engine. But i did know this tricks. Now it will be very helpful.

Posted by: Master Cleanse on 08/04 at 11:40 PM

Finally i have got this. I am really finding this.I have installed Express Engine but did not able to redirect it. but now i am fully clear.Thnx bro

Posted by: Charmine on 08/05 at 05:24 AM

The very great tips is when you have a screen shot that every readers would carefully get the instructions better apply them well to their task, just like this.

Posted by: dantel örnekleri on 08/15 at 04:10 PM

thanks by admin perfect blog
Dantel
Dantel Örnekleri
Dantel Dünyası

Posted by: Keratosis on 08/16 at 02:18 AM

I’ve recently saw an ad about acai berries, and after reading about it I decided I really wanted to try it. Today while at the store I just so happen to see a bottle of acai/pomegranate juice. I was wondering is that still as good for you as just having acai berries? Or is it both good for you while having it together.

Posted by: ugg boots on 08/24 at 02:31 AM

one on one dvd provide other Brands.


so enjoy your shopping here and that’s our biggest wish!radii shoe


These cheap wholesale nfl jerseys  are a ability account and the ideal additive to acceleration up your confidence!


A pair of radii 420  is essential to every people.


This is very good and I am fond of that .Also I total agree. If you would like to learn more, you can click this link:nike dunk


kaya mbt is the hot-selling in the intelnet.

Posted by: Fatal Acne Treatment Mistakes on 08/29 at 02:00 PM

Yep, this helped. I used Redirect 301 by now.
Well done, go on…

Posted by: ibuybattery on 09/02 at 11:28 AM

Very useful article.Surely will visit your blog again later,thanks for sharing.

Page 1 of 1 pages

Post a comment

Name:

Email:

Location:

URL:

Remember my personal information

Notify me of follow-up comments?

Submit the word you see below: