tips icon image

EE Insider Tips
Sponsored by Mijingo's EE 2 Screencasts

ExpressionEngine mini-howtos created by the EE Insider community.

Use conditionals to design your own URL structure

User's avatar
  • Posted by JonnyUK
  • June 08, 2009
  • 6 people have found this tip helpful.

The EE docs state that URL’s at their simplest should be formatted using the following formula:

http://www.example.com/index.php/template_group/template/ 

But what if you want a more customised (or even dynamic) URL that doesn’t necessarily match the template name?

If you try to visit a URL which points to a template that doesn’t exist, such as “http://www.example.com/index.php/template_group/template_doesnt_exist/”, you’ll notice EE will fall-back and default to showing the index template of that template group instead. We can use this to our advantage by making the index template tell EE which templates to use based on whether or not particular segments are present in the URL.

For example, if you wanted to have the following URLs for a car website:

http://www.example.com/index.php/cars/ford/mustang/
http://www.example.com/index.php/cars/fiat/
http://www.example.com/index.php/cars/vauxhall/insignia/ 

‘cars’ being the template_group, and ‘ford’, ‘fiat’, ‘vauxhall’ being the template names…

Yet you don’t want to have to actually create the individual templates ‘ford’, ‘fiat’, ‘vauxhall’ (which would also mean we would have to create new templates when you add a new car make/manufacturer), you could add the following to your index template in the ‘cars’ template group instead:

{if "{segment_3}" != ""}
{embed
="/cars/.car-model"}
{if
:elseif "{segment_2} != ""}
{embed="
/cars/.car-make"}
{if:else}
{embed="
/cars/.cars"}
{/if} 

So, if we visited ‘http://www.example.com/index.php/cars/ford/mustang/’, EE would not find the template ‘ford’, and instead would default to the the index template, which would then recognise that segment_3 is present and would therefore serve up the ‘.car-models’ template in the ‘cars’ template group, which could have the following code in it:

{exp:weblog:entries weblog="car-models" dynamic="off" url_title="{segment_3}"}
{title}
{body}
{
/exp:weblog:entries} 

BUT, if we visited “http://www.example.com/index.php/cars/fiat/”, the index template would recognise that only segment_2 is present and therefore would serve up the ‘.car-make’ template which could use a completely different weblog:

{exp:weblog:entries weblog="car-makes" dynamic="off" url_title="{segment_2}"}
{title}
{
/exp:weblog:entries} 

This is a very handy method that will allow you to create customised and dynamic URL’s while still keeping your pages organised in template groups and templates without the limitations of having to stick the names of templates in your segments.


Important: If you use this method, you will not be able to take advantage of the built-in pagination features as these rely on looking at the URL structure to work out which page is current.

Add Your Comment?

You must have an EE Insider account to post comments on Insider Tips. It's fast, easy and hassle-free.

Sign up now (or login).

Search EE Insider Tips
Sponsored By