1

I'm trying to declare if a user is on a /uk or /us version of the site.

domain.com/uk/ domain.com/us/

{% if (craft.app.request.segments == '/uk/') %}
UK site
{% endif %}

This doesnt work. Can anyone explain why and where I'm going wrong?

Thank you

2

The request segments will come back as an array, where each part between slashes will be an array element so mysite.com/first/second/third gives segments [first,second,third]

If you're just looking for the first segment, you can try (craft.app.request.segments|first == 'uk') to get that information.

| improve this answer | |
  • Unfortunately this doesn't work. – HeyImArt Jun 11 at 14:05
  • That should work. With devMode on, when you do a {{ dump(craft.app.request.segments) }} what are you getting? – RitterKnight Jun 11 at 18:27
2

Assuming you're using Craft's multi-site functionality(?), you'll want to use something like {{ currentSite.handle }} to check which site they're on. For multiple sites configured to use sub-directories from a single domain name, the first segment of the URL is considered to be part of the siteUrl and will not be returned in the segments array.

| 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.