Linked @ del.icio.us

rss icon Mark J. Reeves November, 2007

August 21, 2007 EE: Quick URL redirects using segment variables

This is my second post on using ExpressionEngine to power your blog or web site. ExpressionEngine is a feature-rich content management system used by web designers and developers to power their own and their clients’ web sites. ExpressionEngine works for me as a flexible rendering engine that accommodates custom fields, easy content entry, and a tag-based templating system that anyone can use to store HTML or pre- and post-processed PHP. With ExpressionEngine, I know I’ll never have to say, “sorry, the CMS won’t do that.”

This post came about when I was linked to on the ExpressionEngine Blog.

Happy as I was when I saw traffic start coming through from a link on the ExpressionEngine Blog, I was dismayed to see that my RSS feed, where the link came from, still had my older entry URL, without the /post. My previous design accommodated multiple entries and single entries on the /index template. ExpressionEngine will serve multiple entries as specified on the {exp:weblog:entries} tag, or filter down to a single entry if it catches its url_title on the URL requested.

But single entries weren’t long enough for the homepage in the new design, and I coded all my single entry links to a /post template. I checked and rechecked my homepage links and archive links, but the RSS feed slipped through the cracks. A single entry link off the homepage made it out into the wild, and I was starting to get some decent traffic on a less-than-perfect page.

I shot off an email requesting a change while working on a solution using my old template as a testpad. What did I use? ExpressionEngine’s segment variable.

The segment variable lets you grab a slice of the URL delimited by ‘/’.

On the URL

http://www.c77studios.com/post/ee_aggregating_content_using_magpie_and_expressionengine/

I could output the value ‘post’ by saying

<? echo {segment_1}; ?>

To do so, you need to turn on PHP parsing for the template (Allow PHP?) in the Template Preferences Manager and set the PHP Parsing Stage to Output, so that the PHP code is run on the value output by ExpressionEngine.

My first step was to test things by doing a quick ‘hello world!’.

<?
if ('{segment_1}' == 'ee_aggregating_content_using_magpie_and_expressionengine')
{
echo 'hello world!';
}
?>

That worked, so now I had a functional conditional to catch the value. Since this was an urgent fix, I wanted to catch this specific circumstance and get them to the right spot.

<?
if ('{segment_1}' == 'ee_aggregating_content_using_magpie_and_expressionengine')
{
header('Location: /post/ee_aggregating_content_using_magpie_and_expressionengine/'); exit;
}
?>

That accommodated this circumstance and eliminated the issue. NOTE:

<?
exit;
?>

is necessary in an ExpressionEngine template to suppress the rest of the page when redirecting using header(’Location:url’);

Crisis averted. But what about other URLs out in the wild? How to keep this from happening again, and how to not have single case code sitting around in my template?

<?
if (strlen({segment_1}) > 0) {
header('Location: /post/{segment_1}'); exit;
}
?>

There. Now it’s dynamic. If a request is made 1) on the /index (homepage) template and 2) has a segment variable on it, grab the segment variable and redirect to /post/{segment_1}. Any old URLs out there with the entry-specific url_title directly on the root will bounce to the /post template, right where I want them to be.

UPDATE: Unfortunately this method isn’t foolproof. I had an inbound link with a querystring parameter (c77studios.com?ref=mjr). Even though there’s no ‘/’ on that URL, the link was translated to c77studios.com/?ref=mjr by the browser or server, which created a segment_1 that doesn’t correspond to a post, which redirected to a dead end. I was able to fix the link, but haven’t yet updated the code to handle it.

For more posts about ExpressionEngine, visit http://www.c77studios.com/archives/category/expressionengine.

Read more in the Archives

Mark J. Reeves has been making web sites work since 1998. Currently partnering with designers and firms throughout the Northeast, he pursues front-end development par excellence coupled with experienced database design and development and solid PHP/MySQL or .NET/SQL Server application development. Design-savvy but not a designer, Mark approaches each project enthusiastic about the details and the potential for online success, offering strategic insight on content and marketing decisions.

Mark resides in Salem, Massachusetts with his wife and infant son in a condo that was once a classroom in an 1870s school. With a growing interest in modern architecture, sustainable living and plans to build his own home someday, Mark's also working on a regional community site at ModernHomesNewEngland.com. Get in touch: mjr@c77studios.com.