Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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?

share|improve this question
    
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 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 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 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 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!):

<ul>
    {% nav page in pages %}
        <li>

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

            {% ifchildren %}
                <ul>
                    {% children %}
                </ul>
            {% endifchildren %}

        </li>
    {% endnav %}
</ul>

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') %}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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