Answers.Body: <p>Go to Settings → Routes, and create a route like this:</p>
  
  <p><a href="https://i.sstatic.net/1SjXt.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/1SjXt.png" alt="enter image description here"></a></p>
  
  <p>Then create that <code>partners/_partner_news_entry.html</code> 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.)</p>
  
  <p>In that template, you can get the Partner Reference and the entry slug like this:</p>
  
  <pre><code>{% set partnerRef = matches[1] %}
  {% set entrySlug = matches[2] %}
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>{% set partnerEntry = craft.entries({
      section: 'partners',
      partnerReference: partnerRef
  }).first() %}
  </code></pre>
  
  <p>Make sure it’s a valid Partner Reference like this:</p>
  
  <pre><code>{% if not partnerEntry %}
      {% exit 404 %}
  {% endif %}
  </code></pre>
  
  <p>Do the same stuff for the News entry:</p>
  
  <pre><code>{% set newsEntry = craft.entries({
      section: 'news',
      slug: entrySlug
  }).first() %}
  
  {% if not newsEntry %}
      {% exit 404 %}
  {% endif %}
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>{% set newsEntry = craft.entries({
      section: 'news',
      slug: entrySlug,
      relatedTo: {targetElement: partnerEntry},
  }).first() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-06T20:25:06.820Z
Answers.LastActivityDate: 2016-05-06T20:25:06.820Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <p>I can't see a solid way of setting up routing to handle all of the possible use cases for your URL structure as it is now.</p>
  
  <p>You could set up a dynamic route to look for URI's that are of the form <code>slug/slug</code> and then route them to a custom template where you can then deal with the logic. But this route is very ambiguous and could easily catch false positives.</p>
  
  <p>In my opinion a better way would be to use an extra URI segment in your dynamic route, for example <code>partner/slug/slug</code> or <code>p/slug/slug</code> for brevity. Then you can be sure that this will load the custom template only in the correct use cases.</p>
  
  <p>Note that the <code>slug</code> in the dynamic route above does not necessarily represent a slug, but any string of characters, except for a forward slash. So you can then do whatever you need to in your custom template.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-06T20:21:39.030Z
Answers.LastActivityDate: 2016-05-06T20:21:39.030Z
Answers.UserDisplayName: Ben Croker
Answers.Body: <p><strong>CRAFT CMS 3</strong></p>
  <p>I had a similar issue, and the way I solved it in Craft CMS 3 was through routes, similar to the answer from Brandon Kelly.</p>
  <p>I have the entries <code>location</code>, <code>category</code> and <code>subcategory</code>. These last two without URI (they are just entries without URL).</p>
  <p>But I want to display a template when visiting the routes:</p>
  <p>// Route for menu, categories and subcategories e.g. paris/menu/after-5pm/salads:</p>
  <p><em>location</em>/menu/<em>category/subcategory</em></p>
  <p>// Route for menu and categories e.g. paris/menu/after-5pm:</p>
  <p><em>location</em>/menu/category*</p>
  <p>So what I did in my config/routes.php was:</p>
  <pre><code>&lt;?php
  
  return [
  
      // Route for menu, categories and subcategories e.g. paris/menu/after-5pm/salads
      '&lt;location&gt;/menu/&lt;category&gt;/&lt;subcategory&gt;' =&gt; ['template' =&gt; 'locations/_menu-entry'],
  
      // Route for menu and categories e.g. paris/menu/after-5pm
      '&lt;location&gt;/menu/&lt;category&gt;' =&gt; ['template' =&gt; 'locations/_menu-entry'],
  
  ];
  </code></pre>
  <p>and in my locations/_menu-entry.twig file was:</p>
  <pre><code>{% set locationEntry = craft.entries.section('store').slug(location).one()  %}
  {% set categoryEntry = craft.entries.section('menuCategory').slug(category).one()  %}
  {% set subcategoryEntry = subcategory is defined ? craft.entries.section('menuSubcategories').slug(subcategory).one() : null %}
  
  {% if not locationEntry or not categoryEntry %}
      {% exit 404 %}
  {% endif %}
  
  ... manipulate entries as needed...
  </code></pre>
  <p>Hope this helps!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-13T21:00:28.150Z
Answers.LastActivityDate: 2021-04-15T20:07:45.540Z
Answers.LastEditDate: 2021-04-15T20:07:45.540Z
Answers.UserDisplayName: Paloma Martinez
Body: <p>I have a project which I'm working on which is going to need something similar to the following URL Structure :</p>
  
  <p><code>site.com/dfe45c4f/news_title</code>, <code>site.com/gyb66dx/news_title</code> or <code>site.com/76vvg3s/news_title</code></p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Thanks</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-06T20:00:00.223Z
Comments.Text: Ugghhh, can't you do anything about that URL structure? Even something like `site.com/p/dfe45c4f/news_title` would allow you to easily route it to a template called "p.html" and then perform your logic in there.
Comments.UserDisplayName: Ben Croker
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-06T20:06:35.613Z
Comments.Text: Yes, I _could_... would that be a preferred way of doing it then? Or just easier?
Comments.UserDisplayName: madebyhippo
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-06T20:22:06.550Z
Comments.Text: see my answer which should hopefully help explain
Comments.UserDisplayName: Ben Croker
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-06T21:11:32.403Z
Comments.Text: Rather than considering routing, try an **Entry URL Format** of something like `{{ object.partnerReference }}/{{ slug }}` that just points to a template that contains the logic to apply the correct branding changes. Similar example: http://craftcms.stackexchange.com/questions/13622/how-do-i-properly-use-twig-syntax-in-dynamic-title-url-fields
Comments.UserDisplayName: Erik Reagan
Tags: <routing>
Title: Craft Routing / Variables with Differing URI
ThreadText: # Craft Routing / Variables with Differing URI
  
  (Asked by: username_0 on 2016-05-06)
  
  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
  
  **Tags:** routing
  
  > **Comments:**
  > **username_1**: Ugghhh, can't you do anything about that URL structure? Even something like `site.com/p/dfe45c4f/news_title` would allow you to easily route it to a template called "p.html" and then perform your logic in there.  
  > **username_0**: Yes, I _could_... would that be a preferred way of doing it then? Or just easier?  
  > **username_1**: see my answer which should hopefully help explain  
  > **username_2**: Rather than considering routing, try an **Entry URL Format** of something like `{{ object.partnerReference }}/{{ slug }}` that just points to a template that contains the logic to apply the correct branding changes. Similar example: http://craftcms.stackexchange.com/questions/13622/how-do-i-properly-use-twig-syntax-in-dynamic-title-url-fields  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2016-05-06. Score: 3 | Accepted answer
  
  Go to Settings → Routes, and create a route like this:
  
  [![enter image description here](https://i.sstatic.net/1SjXt.png)](https://i.sstatic.net/1SjXt.png)
  
  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() %}
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2016-05-06. Score: 1
  
  I can't see a solid way of setting up routing to handle all of the possible use cases for your URL structure as it is now.
  
  You could set up a dynamic route to look for URI's that are of the form `slug/slug` and then route them to a custom template where you can then deal with the logic. But this route is very ambiguous and could easily catch false positives.
  
  In my opinion a better way would be to use an extra URI segment in your dynamic route, for example `partner/slug/slug` or `p/slug/slug` for brevity. Then you can be sure that this will load the custom template only in the correct use cases.
  
  Note that the `slug` in the dynamic route above does not necessarily represent a slug, but any string of characters, except for a forward slash. So you can then do whatever you need to in your custom template.
  
  
  ---
  
  ### Answer by username_4 on 2021-04-13. Score: 1
  
  **CRAFT CMS 3**
  
  I had a similar issue, and the way I solved it in Craft CMS 3 was through routes, similar to the answer from username_3.
  
  I have the entries `location`, `category` and `subcategory`. These last two without URI (they are just entries without URL).
  
  But I want to display a template when visiting the routes:
  
  // Route for menu, categories and subcategories e.g. paris/menu/after-5pm/salads:
  
  *location*/menu/*category/subcategory*
  
  // Route for menu and categories e.g. paris/menu/after-5pm:
  
  *location*/menu/category\*
  
  So what I did in my config/routes.php was:
  
  ```
  <?php
  
  return [
  
      // Route for menu, categories and subcategories e.g. paris/menu/after-5pm/salads
      '<location>/menu/<category>/<subcategory>' => ['template' => 'locations/_menu-entry'],
  
      // Route for menu and categories e.g. paris/menu/after-5pm
      '<location>/menu/<category>' => ['template' => 'locations/_menu-entry'],
  
  ];
  
  ```
  
  and in my locations/\_menu-entry.twig file was:
  
  ```
  {% set locationEntry = craft.entries.section('store').slug(location).one()  %}
  {% set categoryEntry = craft.entries.section('menuCategory').slug(category).one()  %}
  {% set subcategoryEntry = subcategory is defined ? craft.entries.section('menuSubcategories').slug(subcategory).one() : null %}
  
  {% if not locationEntry or not categoryEntry %}
      {% exit 404 %}
  {% endif %}
  
  ... manipulate entries as needed...
  
  ```
  
  Hope this helps!
  
  
  ---
  
  