1

I was wondering if there was a way to set a channels page as the default page to load if there is no slug address i.e. I want the below web address to default to a skills page

myWebsite.com/skills

Should direct to

myWebsite.com/skills/Technology

My solution (but I think there is a better way?)

I cheeped out and set the page entry to the page I want to be the default page if the URL meets the requirement:

{% set currentPage = craft.request.url %}

  {% if currentPage == 'https://exampleWebsite.com/skills' %}
    {% set entry = craft.entries.section('skills').slug('technology').one() %}
  {% endif %}

Any suggestions here would be great!

Thanks - W

1 Answer 1

2

Assigning the current entry is not the best approach, as this will mean the same page is served for /skills and /skills/technology - this is bad for SEO. Instead, you can redirect with the redirect tag:

{% redirect '/skills/technology/' 301 %}

Comparing with the full URL is error-prone - what if the URL or the skills slug change? Instead, you want to identify you're on the parent page in a more robust way. How to do that depends on what template the /skills path is routed to.

  • If it's an entry of it's own, I would move the redirect logic out of the template and into the entry content, so editors can change the 'main' skill entry to redirect to. Create an entries field limited to one allowed entry and use this as the redirect target.
  • If it's a custom route, you can route it to a different template from the main skills template and redirect to the technology page instead. You can also use a field in a global set to allow editors to change the main redirect this way.
3
  • This is really great - thanks for the info. As a follow up question (as I am routing the channel as an entry of its own). What do you think is the best option for getting the channels address without the slug i.e. if channel address variable with no slug then redirect? Would you suggest using the redirect entry variables url (using .url) and then splitting the string using '/' as a separator - or is there a dedicated craft call that identifies if there is a slug on this page? Thanks again. W
    – Wally
    Dec 21, 2021 at 11:33
  • 1
    @Wally Check the craft\web\Request class, it has a lot of useful methods to get parts of the URL. But I wouldn't rely on the path at all for redirect logic. Instead, make the condition something that editors can edit. For example, make the entries field optional and only redirect if an entry is selected there. Or only redirect if the entry level is 1. Or something along those lines.
    – MoritzLost
    Dec 21, 2021 at 11:58
  • Amazing thanks for the info and really appreciate the quick reply - legend!
    – Wally
    Dec 21, 2021 at 12:03

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.