<p>Completely possible without any extra routing!</p>
<p>First you would need to make sure the relation fields are required to prevent any errors.</p>
<p>For the <code>Country</code> section, you would simply set up your section with the URI format <code>{ slug }</code></p>
<p>For <code>Region</code> you would set up the URI Format to grab the related Country's slug for the first segment, like this: <code>{ yourCountryFieldHandle.one().slug }/{ slug }</code></p>
<p>And for <code>Town</code> it would be <code>{ yourRegionFieldHandle.one().yourCountryFieldHandle.one().slug }/{ yourRegionFieldHandle.one().slug }/{ slug }</code> which gets the related Region's Country for the first segment, then the related Region for the second.</p>
<p>You can then specify a different template for each section.</p>

CC BY-SA 4.0
2021-01-23T00:09:32.220Z
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
CC BY-SA 4.0
2021-01-25T10:15:35.867Z
You're welcome, good luck!
dmatthams
CC BY-SA 4.0
2021-01-25T10:36:51.130Z
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
CC BY-SA 4.0
2021-01-25T10:54:06.177Z
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
CC BY-SA 4.0
2021-01-22T11:40:54.623Z
2021-01-22T11:40:54.623Z
dmatthams
<p>This may be asking the impossible, but I'm trying to do the following:<br />
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.</p>
<p>I want the URL for a Town entry to look like <a href="https://website.something/uk/west/torquay" rel="nofollow noreferrer">https://website.something/uk/west/torquay</a>, or <a href="https://website.something/portugal/south/faro" rel="nofollow noreferrer">https://website.something/portugal/south/faro</a>. Both of these URLs would load the template specified for the town. A URL of <a href="https://website.something/france/south" rel="nofollow noreferrer">https://website.something/france/south</a> would load a Region template.</p>
<p>I'm guessing I'll have to do some advanced routing?</p>

<craft3><templating><routing>
Dynamic URI to load entry template
# Dynamic URI to load entry template

(Asked by: username_0 on 2021-01-21)

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?

**Tags:** craft3, templating, routing

---

## 1 answers

### Answer by username_1 on 2021-01-22. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_0**: 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!  
> **username_1**: You're welcome, good luck!  
> **username_0**: 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.  
> **username_1**: 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.  

---

