1

I am trying to configure my routes so that the last segment of the URL is ignored and can be used as parameter in the twig file.

for example, the following URL should ignore the city-segment

/services/name-of-the-service/city

and should be routed to

/services/name-of-the-service

I have currenty set up the following routing-rule

'services/<service:{slug}>/<city:{slug}>' => 'service/<service>'

Which results into a 404 and says Unable to resolve the request "services/name-of-the-service". Which is the exactly the URL that I want to get parsed...

When I am trying to route to the template

'services/<service:{slug}>/<city:{slug}>' =>  ['template' => 'services/_entry']

Which seems to load the correct template but the entry can not be parsed...

Any ideas how to solve / work around this problem?

2

I did something similar for a site that showcased projects.

In the top of my entry page, I had code like this:

{% set entrySlug = craft.app.request.getSegment(2) %}
{% set entry = craft.entries.section('services').slug(entrySlug).one() %}

{% if entry is not defined or entry is null %}
    {% exit 404 %}
{% endit %}

And then I've got two routes:

services/<slug>/* which routes to services/_entry

services/<slug> which routes to services/_entry

Might not need both, but for me, when I didn't have both, I'd get a 500 error when the second segment was broken.

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.