Articles
Tidy URLs in ExpressionEngine
ExpressionEngine’s strict template_group/template URL structure allows for some wonderfully tidy content organization. However, an annoying downside to this structure is that EE’s URLs tend to be a bit overgrown. For example, say I have a “blog” template group, and I have a “single.php” template within it that I use to display single posts. The URL would look like this:
http://www.mysite.com/index.php/blog/single/the_title_of_the_post
I don’t know about you, but that index.php in the middle there just hurts my brain. There are a number of techniques to get rid of it involving some .htaccess trickery, but I couldn’t find one that worked properly on my hosting setup. But hey, what about just renaming it? EE has a nice little writeup explaining how to do so. I just renamed my index.php to “v1” (with no extension) and now it looks like a nice directory in the URL. Niggling problem solved.
http://www.mysite.com/v1/blog/single/the_title_of_the_post
See, better already! But wait - there’s plenty more you can do here.
Lose That Redundant Template
The second unnecessary bit here is the “single” template. When you make your EE templates, you might think that it’s necessary to provide it, but, depending on the layout of your site, you may not have to. If you can make your index.php file flexible enough to handle single posts as well, your permalinks become one segment smaller:
http://www.mysite.com/v1/blog/the_title_of_the_post
Here’s what I mean. In most blog designs, there are only a few things that you might want to only display if a visitor is looking at a single post’s page:
- Comment listing & comment entry form
- Next/previous entry links (as opposed to next/previous page links at the bottom of your main post listing page)
- The rest of the post (if your main post listing only displays a summary with a “read more” link)
Well, good news: a single.php template is totally redundant for that — your index.php can totally handle it. The first two bullets are easy:
- For better or worse, EE doesn’t support comments on ‘multi-post’ pages. In this situation, that’s just dandy. Anything within your
comment:entriesorcomment:formtags won’t display unless you’re looking at a single post, so go ahead and embed your .comment template right in there (you are using separate templates and embedding, right?) - Next/previous entry links and pagination (
exp:weblog:next_entryandpaginate) also only display when they’re supposed to, so throw those suckers in there too. You won’t break anything.
All that’s left is any custom code you might only want displayed on the single page, like the ‘read more’ section of the post or a custom header or something. Unfortunately, EE doesn’t have an “if single post is being displayed” conditional, so we’re stuck having to create our own.
This is a tiny bit tricker than it sounds — for example, you could read the URL segment to see if there’s a segment after /blog, but what about pagination, or categories? Both add to that same URL segment.
Here’s the conditional I came up with. It’s long, but it covers all your bases in terms of deducing whether a single post page is being displayed:
if segment_2 AND not_category_request AND total_results == absolute_count
The conditional tests 3 different possibilities:
- It tests for the permalink URL segment, which in this case is
segment_2, the one right after my template group of “blog”. - I’m using the same template for category archive listings, so
not_category_requesttakes care of that. - The final section tests
total_results(posts displayed on the page) vs.absolute_count(all posts in the category/section) to make sure you’re not looking at a paginated post listing that happens to only be displaying one result on that page.
You can just wrap that conditional around the parts of your template that should only display on a single post’s page. Voila - your lone index.php file now handles your main listing, your single posts, and your category listings.
The Best Option, For Now, Anyway
Hopefully EE will throw in a little is_single conditional sometime to handle this (and hey, there’s the chance that they already have and I completely missed it), but for now, that’s the technique I’ve found to work.
So, there’s a few tips on how to trim a bloated ExpressionEngine URL like:
http://mysite.com/index.php/blog/single/the_title_of_the_post
down to a more sensible size:
http://mysite.com/v1/blog/the_title_of_the_post
I hope that does the trick for you. And hey, if you have additional suggestions or techniques to keep EE tidy, I’d love to hear ‘em.
Information
Cameron Daigle is a designer who scribbles information in notebooks, in his head, or (ever so occasionally) on this website.