Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-13T03:02:13.797Z
Answers.Text: getChildren() is being deprecated and relatedTo() is suggested. How would you work this with relatedTo()?
Answers.UserDisplayName: Stephen Callender
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-04T13:35:54.683Z
Answers.Text: @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).
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-29T11:00:27.473Z
Answers.LastActivityDate: 2014-08-20T17:35:01.440Z
Answers.LastEditDate: 2014-08-20T17:35:01.440Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-29T11:41:57.190Z
Comments.Text: 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 :)
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-29T12:38:00.733Z
Comments.Text: @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)
Comments.UserDisplayName: Nick F
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-29T12:43:11.053Z
Comments.Text: 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.
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-29T12:48:51.187Z
Comments.Text: 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!
Comments.UserDisplayName: Nick F
Tags: <routes><navigation><redirect><redirection>
Title: Setting up redirects via Craft routes?
ThreadText: # 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**: username_0, 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, username_0. 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!):
  
  ```
  <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') %}
  
  ```
  
  > **Comments:**
  > **username_2**: getChildren() is being deprecated and relatedTo() is suggested. How would you work this with relatedTo()?  
  > **username_1**: @username_2 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).  
  
  ---
  
  