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?

2 Answers 2


Craft 2: craft.request.getSegment(2), see Craft docs

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


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 [
  '<country_code:[a-z]{2}(-[a-z]{2})?>/<section_URI:team>' => ['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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.