1

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!

2

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.

| improve this answer | |
  • Thanks Clive! I will give it a try! The id in the url is a requirement for this particular project. – Evangelos Jan 21 '18 at 8:08
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-<entrySlug:{slug}>' => 'blog/entry',
'blog/id-<entrySlug:{slug}>' => 'blog/entry',

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

| improve this answer | |

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.