Answers.Body: <p>You need to <a href="https://craftcms.com/docs/routing" rel="nofollow noreferrer">define routes</a> for these URLs in settings in the CP or via the config/routes.php file.</p>
  
  <pre><code>return [
      'team/&lt;regionId:[^\/]+&gt;' =&gt; ['template' =&gt; 'team/_index'],
      'pricing/&lt;regionId:[^\/]+&gt;' =&gt; ['template' =&gt; 'pricing/_index'],
  ];
  </code></pre>
  
  <p>By defining the named subpattern, the template will get loaded with a <code>regionId</code> variable defined.
  Use the <code>is defined</code> 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 <code>entry</code> variable manually, null-coalescing operator again allows you to do that quite elegantly.</p>
  
  <pre><code>{% set entry = entry ?? craft.entries.section('pricing').one() %}
  {% set regionId = regionId ?? 'en' %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-09T16:15:17.343Z
Answers.LastActivityDate: 2017-11-15T11:10:38.617Z
Answers.LastEditDate: 2017-11-15T11:10:38.617Z
Answers.UserDisplayName: carlcs
Body: <p>I have mysite.com and I need to use a segment as if it were a querystring parameter for accomplishing this:</p>
  
  <pre><code>mysite.com/us         -&gt;  same template as: mysite.com
  mysite.com/pricing/bz -&gt;  same template as: mysite.com/pricing
  mysite.com/shops/ag   -&gt;  same template as: mysite.com/shops
  mysite.com/team/mx    -&gt;  same template as: mysite.com/team
  ...etc
  </code></pre>
  
  <p>In my templates I need to know the value of the <code>country code</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.</p>
  
  <p><strong>Note:</strong> This has nothing to do with language. I also have already a multi-site configuration for two languages: mysite.com and mysite.com/es</p>
  
  <p>So I would also need to support these too:</p>
  
  <pre><code>mysite.com/es/us        -&gt;  same template as: mysite.com/es
  mysite.com/es/bz/shops  -&gt;  same template as: mysite.com/es/shops
  mysite.com/es/ag/team   -&gt;  same template as: mysite.com/es/team
  ... etc
  </code></pre>
  
  <p>What's the way to go? How do I tell Craft to render the template for <code>mysite.com/{section_name}</code> when requesting <code>mysite.com/{section_name}/{country_code}</code> So I can use country_code as a parameter from the craft.request segments? I'm struggling with this </p>
  
Tags: <craft3><segments>
Title: Use url segment as querystring parameter [Craft 3]
ThreadText: # Use url segment as querystring parameter [Craft 3]
  
  (Asked by: username_0 on 2017-11-09)
  
  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
  ...etc
  
  ```
  
  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
  
  **Tags:** craft3, segments
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-11-09. Score: 3 | Accepted answer
  
  You need to [define routes](https://craftcms.com/docs/routing) 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' %}
  
  ```
  
  
  ---
  
  