<p>How can I alter the routing in Craft? I need to add a (variable) segment to every possible URL.</p>

<p>The reason is we have a multi lingual site with multiple regions. We need to know the correct region in every template to know which data to load. There are many "multi site/multi region hacks" to find on SE to achieve this, but we don't really like any of them.</p>

<p>We want to add the region segment to the URL after the language segment: eg. <a href="https://myurl.com/en/" rel="nofollow noreferrer">https://myurl.com/en/</a><strong>region</strong></p>

<p>Problem is of course re-routing that URL.</p>

<ol>
<li>One option would be to add <strong>dynamic routing</strong>, but than I need to do this for every template in every language (for translation purposes) and that seems a lot of work. Also, when a new language or template is added later on, all routes must be set anew.</li>
<li>I came upon the <code>getElementRoute</code> <strong>hook</strong>. That allows me to change the URL as I would change it in my Twig template but nothing more it seems.</li>
<li>A <strong>query string</strong> is also an option but that's just not pretty.</li>
</ol>

<p>I would need something like a <em>global dynamic route without a specified template</em> to tell Craft: <em>whatever route you get, just disregard segment 2</em> (region) and do the routing as you would otherwise do.</p>

<p>So, is there a way to add a segment to Craft without messing up all the routing? Maybe some very base level function?</p>

<p><strong>UPDATE</strong></p>

<p>I was thinking about using the <code>getElementRoute()</code> hook to alter the URL by adding the region segment.</p>

<p>And than somehow create a route dynamicaly in the <code>registerSiteRoutes()</code> hook that corresponds to the URL I retrieved in <code>getElementRoute()</code>.</p>

<p>I can do the first part, but not the second.</p>

CC BY-SA 3.0
2017-04-11T22:19:36.577Z
This seems like it might be better handled at the server level. Both nginx and Apache have "rewrite" modules, which could be used to remove the offending URL segment, before the request is processed by Craft.
Stephen Lewis
CC BY-SA 3.0
2017-04-12T09:44:17.460Z
Yes, but I need that segment in my template so I need it in my URL. If I remove it with htaccess, the URL to the website that users share will be without the region segment and the page won't work.
Tom De Smet
CC BY-SA 3.0
2017-04-12T14:03:33.200Z
Oh right, sorry. I misunderstood your requirement.
Stephen Lewis
CC BY-SA 4.0
2018-06-26T08:24:01.840Z
Similar problem here. i just finished the structure of a new site, now i am faced with the Problem of the regions.
i Thought i can use categories for routing. Because i Need: *example.com/REGION/LANGUAGE/about-us* for example. <br/>(or *example.com/fr/en/about-us* in reality - frensh region, english language) - First problem: Localize all and get the language paramter in the URL - CHECK, this is done (i got *example.com/en/about-us* - quite easy) - Second problem is, how to get the category (top level) into the routing (not done, i am struggeling with this) - Third problem - and maybe biggest
Thomas
CC BY-SA 4.0
2018-06-27T08:51:53.303Z
Are you using Craft 2 or 3?
Tom De Smet
<routes><routing>
Alter the routing in Craft for every URL independent of language
# Alter the routing in Craft for every URL independent of language

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

How can I alter the routing in Craft? I need to add a (variable) segment to every possible URL.

The reason is we have a multi lingual site with multiple regions. We need to know the correct region in every template to know which data to load. There are many "multi site/multi region hacks" to find on SE to achieve this, but we don't really like any of them.

We want to add the region segment to the URL after the language segment: eg. <https://myurl.com/en/>**region**

Problem is of course re-routing that URL.

1. One option would be to add **dynamic routing**, but than I need to do this for every template in every language (for translation purposes) and that seems a lot of work. Also, when a new language or template is added later on, all routes must be set anew.
2. I came upon the `getElementRoute` **hook**. That allows me to change the URL as I would change it in my Twig template but nothing more it seems.
3. A **query string** is also an option but that's just not pretty.

I would need something like a *global dynamic route without a specified template* to tell Craft: *whatever route you get, just disregard segment 2* (region) and do the routing as you would otherwise do.

So, is there a way to add a segment to Craft without messing up all the routing? Maybe some very base level function?

**UPDATE**

I was thinking about using the `getElementRoute()` hook to alter the URL by adding the region segment.

And than somehow create a route dynamicaly in the `registerSiteRoutes()` hook that corresponds to the URL I retrieved in `getElementRoute()`.

I can do the first part, but not the second.

**Tags:** routes, routing

> **Comments:**
> **username_1**: This seems like it might be better handled at the server level. Both nginx and Apache have "rewrite" modules, which could be used to remove the offending URL segment, before the request is processed by Craft.  
> **username_0**: Yes, but I need that segment in my template so I need it in my URL. If I remove it with htaccess, the URL to the website that users share will be without the region segment and the page won't work.  
> **username_1**: Oh right, sorry. I misunderstood your requirement.  
> **username_2**: Similar problem here. i just finished the structure of a new site, now i am faced with the Problem of the regions. i Thought i can use categories for routing. Because i Need: *example.com/REGION/LANGUAGE/about-us* for example. <br/>(or *example.com/fr/en/about-us* in reality - frensh region, english language) - First problem: Localize all and get the language paramter in the URL - CHECK, this is done (i got *example.com/en/about-us* - quite easy) - Second problem is, how to get the category (top level) into the routing (not done, i am struggeling with this) - Third problem - and maybe biggest  
> **username_0**: Are you using Craft 2 or 3?  

---

## 0 answers

