<p>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!):</p>

<pre><code>&lt;ul&gt;
    {% nav page in pages %}
        &lt;li&gt;

            {% if page.getChildren()|length %}
                &lt;a href="{{ page.getChildren().first().url }}"&gt;{{ page.title }}&lt;/a&gt;
            {% else %}
                &lt;a href="{{ page.url }}"&gt;{{ page.title }}&lt;/a&gt;
            {% endif %}

            {% ifchildren %}
                &lt;ul&gt;
                    {% children %}
                &lt;/ul&gt;
            {% endifchildren %}

        &lt;/li&gt;
    {% endnav %}
&lt;/ul&gt;
</code></pre>

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

<pre><code>{% if page.getChildren()|length and page.id('and, not 1, not 2, not 3') %}
</code></pre>

CC BY-SA 3.0
2015-02-13T03:02:13.797Z
getChildren() is being deprecated and relatedTo() is suggested. How would you work this with relatedTo()?
Stephen Callender
CC BY-SA 3.0
2015-03-04T13:35:54.683Z
@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"](http://craftcms.stackexchange.com/q/5072/125).
carlcs
CC BY-SA 3.0
2014-07-29T11:00:27.473Z
2014-08-20T17:35:01.440Z
2014-08-20T17:35:01.440Z
carlcs
<p>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?</p>

<p>I know I can use Twig's <code>{% redirect %}</code> 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?</p>

CC BY-SA 3.0
2014-07-29T11:41:57.190Z
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
CC BY-SA 3.0
2014-07-29T12:38:00.733Z
@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
CC BY-SA 3.0
2014-07-29T12:43:11.053Z
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
CC BY-SA 3.0
2014-07-29T12:48:51.187Z
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
<routes><navigation><redirect><redirection>
Setting up redirects via Craft routes?
# Setting up redirects via Craft routes?

(Asked by: username_0 on 2014-07-29)

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?

**Tags:** routes, navigation, redirect, redirection
> **Comments:**
> **username_1**: 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 :)  
> **username_0**: @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)  
> **username_1**: 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.  
> **username_0**: 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!  

---

## 1 answers

### Answer by username_1 on 2014-07-29. Score: 4

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 %}
                {{ page.title }}
            {% else %}
                {{ page.title }}
            {% 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') %}

> **Comments:**
> **username_2**: getChildren() is being deprecated and relatedTo() is suggested. How would you work this with relatedTo()?  
> **username_1**: @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"](http://craftcms.stackexchange.com/q/5072/125).  

---

