This may be asking the impossible, but I'm trying to do the following:
Say I have 3 channels, Country, Region, and Town. Town has an entry field to select a Region, a Region entry has a field to select a Country.

I want the URL for a Town entry to look like https://website.something/uk/west/torquay, or https://website.something/portugal/south/faro. Both of these URLs would load the template specified for the town. A URL of https://website.something/france/south would load a Region template.

I'm guessing I'll have to do some advanced routing?


Completely possible without any extra routing!

First you would need to make sure the relation fields are required to prevent any errors.

For the Country section, you would simply set up your section with the URI format { slug }

For Region you would set up the URI Format to grab the related Country's slug for the first segment, like this: { yourCountryFieldHandle.one().slug }/{ slug }

And for Town it would be { yourRegionFieldHandle.one().yourCountryFieldHandle.one().slug }/{ yourRegionFieldHandle.one().slug }/{ slug } which gets the related Region's Country for the first segment, then the related Region for the second.

You can then specify a different template for each section.

  • Wow! It's a bit late for me, but doing a quick test this looks to work exactly how I'm hoping! I'll give it a better check on Monday, and will mark as answer when I'm more awake. Either way, thank you! – Jaytrix Jan 23 at 0:09
  • You're welcome, good luck! – dmatthams Jan 25 at 10:15
  • Seems all good so far. Need to test some more, but so far it's exactly what I'm after. Thank you! On a side note, it's dramatically changed how I think the URI format works. I'm sure this will help with other projects. Re-reading the docs I can see how your answer works, but I think you have it better explained. – Jaytrix Jan 25 at 10:36
  • Yes – it was a big deal when I realised I could use field values in URL format, and dynamic entry titles. The second best option would be to use the same template for all sections that reads segment 2+3 for region/town and changes the template appropriately (or redirects to another template). You could do it with routing for sure, but you'd miss out on the tighter integration with Craft – so no live preview etc. – dmatthams Jan 25 at 10:54

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.