<p>I'd probably setup the following routes:</p>

<pre><code>'blog/*/*' =&gt; 'blog/entry',
'blog/*' =&gt; 'blog/entry',
</code></pre>

<p>And then on the entry template I'd get the last segment and <a href="https://twig.symfony.com/doc/2.x/filters/split.html" rel="nofollow noreferrer">split it</a> to get the ID like so:</p>

<pre><code>{% set segment = craft.request.lastSegment | split('-') %}
{% set id = segment[0] %}
{% set entry = craft.entries.id(id).first %}
</code></pre>

<p>That should work for either of your {id}-{slug} examples. Though in practice I'd probably fetch the entry using the slug and not the ID as I think it looks prettier and I don't like IDs in URLs.</p>

CC BY-SA 3.0
2018-01-21T08:08:14.723Z
Thanks Clive! I will give it a try! The id in the url is a requirement for this particular project.
Evangelos
CC BY-SA 3.0
2018-01-19T10:49:55.310Z
2018-01-19T10:49:55.310Z
Clive Portman
<p>I've just been trying to figure this out myself and have a slightly different approach that might work the same with a little less code.</p>

<pre><code>'blog/*/id-&lt;entrySlug:{slug}&gt;' =&gt; 'blog/entry',
'blog/id-&lt;entrySlug:{slug}&gt;' =&gt; 'blog/entry',
</code></pre>

<p>The 'slug' variable with it's value from the url will be available in the template without having to do anything else.</p>

CC BY-SA 4.0
2018-09-24T05:08:53.230Z
2018-09-24T05:20:14.723Z
2018-09-24T05:20:14.723Z
CarbonCoyote
<p>I am trying to do this. Have a structure url like so: /{id}-{slug} and /{parent.id}/{id}-{slug}</p>

<p>I need the slug on the url but I only for SEO, I want craft to ignore the slug and only go for the ID. So even though it is /{id}-{slug} for craft it would be /{id} and so on.</p>

<p>What would be the recommended approach? Advanced routing? Manually remove the -{slug} and search by {id} on the template? Any other way?</p>

<p>Cheers!</p>

<url><routes><slug>
Advanced routing
# Advanced routing

(Asked by: username_0 on 2018-01-18)

I am trying to do this. Have a structure url like so: /{id}-{slug} and /{parent.id}/{id}-{slug}

I need the slug on the url but I only for SEO, I want craft to ignore the slug and only go for the ID. So even though it is /{id}-{slug} for craft it would be /{id} and so on.

What would be the recommended approach? Advanced routing? Manually remove the -{slug} and search by {id} on the template? Any other way?

Cheers!

**Tags:** url, routes, slug


---

## 2 answers

### Answer by username_1 on 2018-01-19. Score: 2 | Accepted answer

I'd probably setup the following routes:
'blog/*/*' => 'blog/entry',
'blog/*' => 'blog/entry',

And then on the entry template I'd get the last segment and split it to get the ID like so:
{% set segment = craft.request.lastSegment | split('-') %}
{% set id = segment[0] %}
{% set entry = craft.entries.id(id).first %}

That should work for either of your {id}-{slug} examples. Though in practice I'd probably fetch the entry using the slug and not the ID as I think it looks prettier and I don't like IDs in URLs.

> **Comments:**
> **username_0**: Thanks Clive! I will give it a try! The id in the url is a requirement for this particular project.  

---

### Answer by username_2 on 2018-09-24. Score: 1

I've just been trying to figure this out myself and have a slightly different approach that might work the same with a little less code.
'blog/*/id-' => 'blog/entry',
'blog/id-' => 'blog/entry',

The 'slug' variable with it's value from the url will be available in the template without having to do anything else.


---

