URL Segment Variables¶
ExpressionEngine provides an easy way for you to access the information in your URL segments. By “segments”, we mean the segments of the URL that appear after your index.php page. For example, this URL has two segments:
http://example.com/index.php/products/shirts/
products is Segment 1 and shirts is Segment 2.
Up to 9 URL segments can be accessed using the following variables:
{segment_1} {segment_2} {segment_3} {segment_4} {segment_5} {segment_6} {segment_7} {segment_8} {segment_9}
Segment variables let you dynamically change aspects of your templates based on what appears in the URL.
For example, imagine you use an ExpressionEngine channel to store information about each employee in your company. Each channel entry describes a different person, with the URL Title of the entry being the person’s name. In this scenario you could use a single template to dynamically show each employee’s information based on what is in the URL. Consider this URL:
http://example.com/index.php/company/employees/joe/
The Template Groups name is “company” and the Template name is “employees”. Based on the information in the 3rd segment (in this case, “joe”) you can dynamically cause the “employees” template to change for each person. Here’s an example of a channel tag in which the URL title changes based on the 3rd segment:
{exp:channel:entries url_title="{segment_3}"}
<h1>{title}</h1>
<p>{body}</p>
{/exp:channel:entries}
Last Segment¶
The {last_segment} global variable gives you the ability to determine the last segment of your URL when the number of URI segments is unknown.
http://example.com/index.php/company/
{if last_segment == 'joe'} Hey Joe, where you goin' with that? {/if}
http://example.com/index.php/company/employees/joe
Hey {last_segment}, where you goin' with that?

User Contributed Notes
URLs with more than 9 segments will return “Error: The URL contains too many segments”. EE will also send a header with “500 Internal Server Error”.
You can also access URI segments via PHP as follows. This is helpful when you want to dynamically write out parts of a tag before processing.
<?
$this->EE =& get_instance();
$seg1 = $this->EE->uri->segment(1);
$seg2 = $this->EE->uri->segment(2);
if ($seg2 != '' && $seg1 == 'foo') { // let sub pages show 25 items
?>
{exp:channel:entries limit="25"}
<? } else { // but the main page shows only 5
?>
{exp:channel:entries limit="5"}
<? } ?>
... the rest of the channel loop goes here
More information is here: http://ellislab.com/expressionengine/user-guide/development/usage/uri.html
You must have an EllisLab product license and have at least 50 posts to the community forums to contribute notes to the User Guide