I have mysite.com and I need to use a segment as if it were a querystring parameter for accomplishing this:

mysite.com/us         ->  same template as: mysite.com
mysite.com/pricing/bz ->  same template as: mysite.com/pricing
mysite.com/shops/ag   ->  same template as: mysite.com/shops
mysite.com/team/mx    ->  same template as: mysite.com/team

In my templates I need to know the value of the country code used in the first segment for using country-specific values for small details like illustrations and prices. Just as if it was a querystring parameter, but it has to be a segment.

Note: This has nothing to do with language. I also have already a multi-site configuration for two languages: mysite.com and mysite.com/es

So I would also need to support these too:

mysite.com/es/us        ->  same template as: mysite.com/es
mysite.com/es/bz/shops  ->  same template as: mysite.com/es/shops
mysite.com/es/ag/team   ->  same template as: mysite.com/es/team
... etc

What's the way to go? How do I tell Craft to render the template for mysite.com/{section_name} when requesting mysite.com/{section_name}/{country_code} So I can use country_code as a parameter from the craft.request segments? I'm struggling with this 😕

1 Answer 1


You need to define routes for these URLs in settings in the CP or via the config/routes.php file.

return [
    'team/<regionId:[^\/]+>' => ['template' => 'team/_index'],
    'pricing/<regionId:[^\/]+>' => ['template' => 'pricing/_index'],

By defining the named subpattern, the template will get loaded with a regionId variable defined. Use the is defined test or the null-coalescing operator to see if the template got loaded via your custom route or via the entry route. In the case that the template gets loaded via the custom route you also need to define the entry variable manually, null-coalescing operator again allows you to do that quite elegantly.

{% set entry = entry ?? craft.entries.section('pricing').one() %}
{% set regionId = regionId ?? 'en' %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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