<p>If it's just a simple redirect, it'll be quicker to do it in .htaccess and not invoke PHP/Craft at all.</p>

CC BY-SA 3.0
2014-10-06T15:49:26.600Z
Hey Brad, is this also how you do it with the structure on buildwithcraft.com → Documentation → Installing and Updating?
carlcs
CC BY-SA 3.0
2014-10-06T15:53:28.090Z
No it's not, just checked the urls in the code. :D
carlcs
CC BY-SA 3.0
2014-10-06T15:42:57.650Z
2014-10-06T15:42:57.650Z
Brad Bell
<p>We had a pretty similar question already: <a href="https://craftcms.stackexchange.com/q/1488/125">"Setting up redirects via Craft routes?"</a>. </p>

<p>Here's how you could change the code to exchange the parent entry with its first child conditionally. Additionally you can also add a <a href="http://buildwithcraft.com/docs/templating/redirect" rel="nofollow noreferrer"><code>redirect</code></a> to that child page, in case someone edits the url.</p>

<pre><code>{% set linkToChildEntries = ['exhibitors', 'anotherparent'] %}

&lt;ul&gt;
    {% nav page in pages %}
        &lt;li&gt;

            {% if page.slug in linkToChildEntries and 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>Of course you can also make this client editable by adding a field to the parent's entry type you then check for instead of managing the to be treated parent entires in the template code.</p>

CC BY-SA 3.0
2014-10-06T15:50:55.990Z
2014-10-06T15:56:12.183Z
2017-04-13T12:51:00.253Z
carlcs
<p>I don't have any of the code to hand so I'll have to check it later, but I had a similar need and wanted something I could switch on and off across multiple entries, so I went with a <a href="http://buildwithcraft.com/docs/lightswitch-fields" rel="nofollow">lightswitch field</a> and the following code in the template:</p>

<pre><code>{% if entry.firstChildRedirect %}
    {% if entry.children|length %}
        {% redirect entry.children.first.uri %}
    {% endif %}
{% endif %}
</code></pre>

<p>I've also got a <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow">function in a variable</a> that when given an entry, either returns that entry, or it's first child, using similar logic.</p>

<pre><code>&lt;?php
namespace Craft;

class YourPluginVariable
{
    public function entryOrRedirected(EntryModel $entry)
    {
        if($entry-&gt;firstChildRedirect)
        {
            $child = $entry-&gt;children-&gt;first();

            if($child) return $child;
        }

        return $entry;
    }
}
</code></pre>

<p>Which can be used in your templates:</p>

<pre><code>{{ craft.yourPlugin.entryOrRedirected(entry).getLink }}
</code></pre>

CC BY-SA 3.0
2014-10-06T16:14:23.787Z
2014-10-06T16:14:23.787Z
Mike Pepper
<p>Client has created /exhibitors but wishes it to point to /exhibitors/partners</p>

<p>Can that be done using routes or should we be looking at htaccess rules?</p>

<structure><routes><routing><htaccess>
redirect on a Structure
# redirect on a Structure

(Asked by: username_0 on 2014-10-06)

Client has created /exhibitors but wishes it to point to /exhibitors/partners

Can that be done using routes or should we be looking at htaccess rules?

**Tags:** structure, routes, routing, htaccess


---

## 3 answers

### Answer by username_1 on 2014-10-06. Score: 3 | Accepted answer

If it's just a simple redirect, it'll be quicker to do it in .htaccess and not invoke PHP/Craft at all.

> **Comments:**
> **username_2**: Hey Brad, is this also how you do it with the structure on buildwithcraft.com → Documentation → Installing and Updating?  
> **username_2**: No it's not, just checked the urls in the code. :D  

---

### Answer by username_2 on 2014-10-06. Score: 2

We had a pretty similar question already: "Setting up redirects via Craft routes?". 
Here's how you could change the code to exchange the parent entry with its first child conditionally. Additionally you can also add a redirect to that child page, in case someone edits the url.
{% set linkToChildEntries = ['exhibitors', 'anotherparent'] %}


    {% nav page in pages %}
        

            {% if page.slug in linkToChildEntries and page.getChildren()|length %}
                {{ page.title }}
            {% else %}
                {{ page.title }}
            {% endif %}

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

        
    {% endnav %}


Of course you can also make this client editable by adding a field to the parent's entry type you then check for instead of managing the to be treated parent entires in the template code.


---

### Answer by username_3 on 2014-10-06. Score: 1

I don't have any of the code to hand so I'll have to check it later, but I had a similar need and wanted something I could switch on and off across multiple entries, so I went with a lightswitch field and the following code in the template:
{% if entry.firstChildRedirect %}
    {% if entry.children|length %}
        {% redirect entry.children.first.uri %}
    {% endif %}
{% endif %}

I've also got a function in a variable that when given an entry, either returns that entry, or it's first child, using similar logic.
firstChildRedirect)
        {
            $child = $entry->children->first();

            if($child) return $child;
        }

        return $entry;
    }
}

Which can be used in your templates:
{{ craft.yourPlugin.entryOrRedirected(entry).getLink }}


---

