<p>There's also another way that would be more consistent, in case your segments are <strong>not always</strong> in the same segments array <strong>position</strong>.
You'd make your routes to match a certain mask in the <strong>routes.php</strong> file like so:</p>

<pre><code>return [
  '&lt;country_code:[a-z]{2}(-[a-z]{2})?&gt;/&lt;section_URI:team&gt;' =&gt; ['template' =&gt; 'team']
];
</code></pre>

<p>And then in your <strong>twig</strong> template you could access the variables <code>country_code</code> and <code>section_URI</code> as long as these two were provided in the URL, otherwise they'll be undefined, which you can also check.</p>

<pre><code>{% if country_code is defined %}
   //Do something with country_code
{% endif %}
</code></pre>

<p><strong>Note:</strong> The syntax used in Craft 3 routing differs from the one in Craft 2: <a href="https://docs.craftcms.com/v3/changes-in-craft-3.html#url-rules" rel="nofollow noreferrer">https://docs.craftcms.com/v3/changes-in-craft-3.html#url-rules</a></p>

CC BY-SA 4.0
2017-11-23T10:45:33.653Z
2018-11-02T18:50:27.123Z
2018-11-02T18:50:27.123Z
Rosana Ruiz
<p>Craft 2: <code>craft.request.getSegment(2)</code>, see <a href="https://docs.craftcms.com/v2/templating/craft.request.html#getsegment-n" rel="noreferrer">Craft docs</a></p>

<p>Craft 3: <code>craft.app.request.getSegment(2)</code></p>

CC BY-SA 4.0
2017-11-23T10:37:37.470Z
2019-06-17T14:37:31.707Z
2019-06-17T14:37:31.707Z
CreateSean
<p>I can see that <code>craft.request.firstSegment</code> and <code>craft.request.lastSegment</code> works but <code>craft.request.secondSegment</code> does not. Is there a way (as in ExpressionEngine) to get the value of any URL segment? </p>

<segments>
How to get a URL segment
# How to get a URL segment

(Asked by: username_0 on 2017-11-23)

I can see that `craft.request.firstSegment` and `craft.request.lastSegment` works but `craft.request.secondSegment` does not. Is there a way (as in ExpressionEngine) to get the value of any URL segment?

**Tags:** segments


---

## 2 answers

### Answer by username_2 on 2017-11-23. Score: 14 | Accepted answer

Craft 2: `craft.request.getSegment(2)`, see [Craft docs](https://docs.craftcms.com/v2/templating/craft.request.html#getsegment-n)

Craft 3: `craft.app.request.getSegment(2)`


---

### Answer by username_1 on 2017-11-23. Score: 6

There's also another way that would be more consistent, in case your segments are not always in the same segments array position.
You'd make your routes to match a certain mask in the routes.php file like so:
return [
  '/' => ['template' => 'team']
];

And then in your twig template you could access the variables country_code and section_URI as long as these two were provided in the URL, otherwise they'll be undefined, which you can also check.
{% if country_code is defined %}
   //Do something with country_code
{% endif %}

Note: The syntax used in Craft 3 routing differs from the one in Craft 2: https://docs.craftcms.com/v3/changes-in-craft-3.html#url-rules


---

