1

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.

  • 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. – Stephen Lewis Apr 11 '17 at 22:19
  • 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 Apr 12 '17 at 9:44
  • Oh right, sorry. I misunderstood your requirement. – Stephen Lewis Apr 12 '17 at 14:03
  • 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 Jun 26 '18 at 8:24
  • Are you using Craft 2 or 3? – Tom De Smet Jun 27 '18 at 8:51

Your Answer

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

Browse other questions tagged or ask your own question.