Craft Routing / Variables with Differing URI I have a project which I'm working on which is going to need something similar to the following URL Structure :

site.com/dfe45c4f/news_title, site.com/gyb66dx/news_title or site.com/76vvg3s/news_title

The First URI Segment is the 'Partner Reference' - a hash value for every partner we work with, the second is a simple entry url title. Every page will show the same data, but each page will be 'branded' differently based on the Partner Reference.

What I need to do is to query a Craft channel a set a variety of variables (styling/logo/branding etc) for the page based on the partner reference.

How would I set routing to look up the first Segment URI to look at the partner section and return all the field information as variables?

Thanks
Go to Settings → Routes, and create a route like this:



Then create that partners/_partner_news_entry.html template. (You can change the location/name of the template if you want; just make sure it matches the “Load this template” setting within your route.)

In that template, you can get the Partner Reference and the entry slug like this:

{% set partnerRef = matches[1] %}
{% set entrySlug = matches[2] %}


I’m not sure how you have it set up, but assuming you have a “Partners” channel with a “Partner Reference” custom field, which stores all of the styling/logo/branding info you need, you can get those relevant entries like this:

{% set partnerEntry = craft.entries({
    section: 'partners',
    partnerReference: partnerRef
}).first() %}


Make sure it’s a valid Partner Reference like this:

{% if not partnerEntry %}
    {% exit 404 %}
{% endif %}


Do the same stuff for the News entry:

{% set newsEntry = craft.entries({
    section: 'news',
    slug: entrySlug
}).first() %}

{% if not newsEntry %}
    {% exit 404 %}
{% endif %}


If there’s an Entries field that actually ties the News entries to a specific partner and you want to ensure that you’re not viewing a News entry that should have only been associated with someone else, you can tweak that like this:

{% set newsEntry = craft.entries({
    section: 'news',
    slug: entrySlug,
    relatedTo: {targetElement: partnerEntry},
}).first() %}