I'm trying to set up redirects for some pages (specifically pages in structure sections that are there purely for structure, which I want to redirect to their first child). Craft's "routes" seem to be geared towards routing a request to a template. But how can I simply route a request to a different URL?

I know I can use Twig's {% redirect %} function, but to be honest I'd much rather just set up all the routes I want redirected outside my template (in order to allow me to avoid (hopefully) unnecessary conditional URL-handling logic in the template). Is this possible at all? If not, is there an elegant way of handling it inside a template?

  • Nick, I added the 'navigation' tag to your question. I am not sure if your problem is solved with an approach changing the navigation template only. If not, feel free to remove it again :)
    – carlcs
    Jul 29, 2014 at 11:41
  • @ChristianSeelbach: ha, thanks, actually your answer is potentially an elegant alternative solution to the particular problem I'm having, but I guess the question was really meant to be more generally about routing... (I'd still like to know how to easily handle URL re-routing)
    – Nick F
    Jul 29, 2014 at 12:38
  • I wouldn't want to set all the routes up in the CP, Nick. So adding some logic to the templates, as you described, is probably the way to go.
    – carlcs
    Jul 29, 2014 at 12:43
  • Is there really no way of setting up simple URL redirects in Craft? I'm used to frameworks like FuelPHP where setting up a bunch of routes to handle this sort of thing is really straightforward, and at least looks like Craft's "Routes". What confuses me is why the end of a Craft "route" seems to always be a template rather than a URL... I guess I'd probably put these redirects in the .htaccess rather than the template if I really have to hard-code them - unless there's an elegant way of doing it in the template - but it would be so much nicer to be able to manage these via the admin panel!
    – Nick F
    Jul 29, 2014 at 12:48

1 Answer 1


If you want to "redirect" all parent entries of your structure that have child entries, you could do something like this in your navigation (untested code!):

    {% nav page in pages %}

            {% if page.getChildren()|length %}
                <a href="{{ page.getChildren().first().url }}">{{ page.title }}</a>
            {% else %}
                <a href="{{ page.url }}">{{ page.title }}</a>
            {% endif %}

            {% ifchildren %}
                    {% children %}
            {% endifchildren %}

    {% endnav %}

If you want to exclude specific parent entries from that behavior, you could do this like so:

{% if page.getChildren()|length and page.id('and, not 1, not 2, not 3') %}
  • getChildren() is being deprecated and relatedTo() is suggested. How would you work this with relatedTo()? Feb 13, 2015 at 3:02
  • @Stephen sorry, I somehow missed answering you. You probably use getChildren with entries that have no child entries. See the discussion here: "How to search children with relatedTo param".
    – carlcs
    Mar 4, 2015 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.