Ever wanted these kind of URL's?
http://www.mydomain.com/article/this_is_my_post_title
http://www.mydomain.com/product/a_very_good_product
(article/product being the template group)
Instead of:
Http://www.mydomain.com/article/comment/this_is_my_post_title
http://www.mydomain.com/product/details/a_very_good_product
So let's say the URL we want is www.domain.com/article/post_title. ExpressionEngine normally would think that 'post_title' is a template inside of the 'article' template group. If you go to domain.com/article/post_title and ExpressionEngine does not find the template 'post_title' it will use the 'index' template instead. Here is where the magic happens! Since ExpressionEngine will serve the index template instead, there we will have a conditional waiting to check if segment 2 of the URL is empty (then it will embed/serve index2 template), and if segment 2 of the URL is not empty it will embed/serve the 'details' template. 'Details' template is in this example the 'full entry template'.
Still with me? This method even allows us to keep using normal templates in the same template group. Lets say we have a 'feed' template in our 'article' template group, ExpressionEngine will still serve the 'feed' template if we go to: domain.com/article/feed/. So it is basically foolproof. Well in rare situations it's not, let us see the advantages and drawbacks.
Advantages:
1. Shorter URL's
2. Keep using templates like you always would.
3. More search engine friendly URL's
4. It's just sexy =)
Drawbacks:
1. You can't have the same URL title as a template name in the same template group. (99% of the time this isn't a issue.)
2. You must have unique URL titles if you want to use the same template group for multiple weblogs. (99% of the time this isn't a issue either.)
For this tutorial we are going to assume we want to use: http://www.mydomain.com/articles/a_post_title as our url.
1. We are going to create a template group called 'articles'.
2. Inside of the 'articles' group we are going to create some normal templates.
3. After that we fill them with some tags and conditionals.
Without further delay let's start!
So let's start with the templates.
1. Create a template group called 'articles'. (Note: remove the quotes)
2. Inside of that group we are going to create 3 templates: '.index2' '.details' 'feed' (notice the dots, these dots make a template hidden so the outside world can't see/use them)
Note: you can name your templates anything you want, I will just use these names for this tutorial.
So now we have a 'index' '.index2' '.details' 'feed' template inside of our 'articles' template group.
-The 'index' template is the template we are going to use where we have conditionals to tell ExpressionEngine which template to embed when it gets requested.
-The '.index2' is the template that is going to get embedded when there is no segment 2 in the url (domain.com/articles/).
-The '.details' template is our 'full entry' template, this template gets embedded when segment 2 is NOT empty (mydomain.com/articles/my_post_title).
-The 'feed' template is a regular template you can use for anything you want, this template will get served when segment 2 equals to 'feed' (mydomain.com/articles/feed). This template is just to let you see that you can keep using normal templates in the same template group as 'article'. Cause only when ExpressionEngine does not find the template defined in segment 2 of the URL will it use the 'index' template. So if segment 2 is 'feed' and you have a template called 'feed' it will get served normally.
Here we will have conditionals to check which template to embed. Paste the following code inside of the 'index' template and hit update.
{if segment_2 != ""} {embed="articles/.details"} {if:else} {embed="articles/.index2"} {/if}
This template will get embedded inside of the 'index' template when segment 2 of the URL is empty. Paste the following code and hit update.
<HTML> <HEAD> <TITLE>This is the index2 template</TITLE> </HEAD> <BODY> <H2>This template will only get served when segment 2 of the URL is empty</H2> </BODY> </HTML>
This template will get embedded inside of the 'index' template when segment 2 of the URL is NOT empty. Here we have our 'full entry' page in action. Paste the following code and hit update.
<HTML> <HEAD> <TITLE>This is the details template</TITLE> </HEAD> <BODY> <H1>This template will only get served when segment 2 of the URL is NOT empty</H1> <BR /> {exp:weblog:entries url_title="{segment_2}" weblog="myweblog" dynamic="off"} < H3>{title}</H3> <P>{summary}</P> <P>{extended}</P> {/exp:weblog:entries} </BODY> </HTML>
This is just a normal template to let you see that you can continue to use your normal templates in the same template group. Fill it with anything you want.
Other useful things you can do with this.
-In a multi language site you could have a conditional checking for a segment of the URL to find 'SP' (Spanish - just a example) and make it display a other template instead of the standard one.
-Or have a conditional checking for the referrer and make a decision based on that.
-You can also do a IP/country check and make a conditional based on that.
The possibilities are endless.
Want to see it in action? Well this website makes use of this method..as you see i got short URL's and it works like a charm.