<p>I'm not sure I understand your question, but to try:</p>

<p>There actually isn't a "standard Craft routing", for channel Sections or anything else.</p>

<p>In your channel's Section settings (available via <em>Settings -> Sections -> [your event section]</em>), there's a field called <strong>Entry URL format</strong>. Simply add <code>{slug}</code> to that field (and nothing else), and you should be good to go. You can even use the same URL format for different sections.</p>

<p>In regards to your "bonus question", if you set the Entry URL format to simply <code>{slug}</code> for the events section, Craft will probably throw 404 for any <code>/events/{slug}</code> URLs, out of the box.</p>

<p>As for <a href="https://craftcms.com/docs/routing#dynamic-routes" rel="nofollow noreferrer">dynamic routes</a>, you're correct that Craft won't populate your template with the <code>entry</code> variable – it will however, populate the template with variables matching the <em>tokens</em> that you add to your route. So, if you e.g. create a dynamic route that uses the <code>&lt;slug&gt;</code> token, you can pull the entry in the template like this:</p>

<pre><code>{# At the top of your template: #}

{% set entry = craft.entries.section('events').slug(slug).first() %}
{% if not entry %}
   {% exit 404 %}
{% endif %}

{# ...the rest of your template #}
</code></pre>

<p>Beyond the above – if you're still unsure how to set things up properly, I'd suggest taking a look at <a href="https://craftcms.com/docs/routing" rel="nofollow noreferrer">the official docs on routing</a>, which explains in detail how Craft evaluates requests and what your different routing options are. Particularly important if you figure that you <em>do</em> need to do some custom routing beyond what the URL format setting can do.</p>

CC BY-SA 3.0
2017-01-18T18:11:39.600Z
2017-01-18T21:54:56.340Z
2017-01-18T21:54:56.340Z
Mats Mikkel Rummelhoff
<p>For a project I have, I am unable to use the standard Craft routing for a channel. For example,</p>

<pre><code>/events/{slug}  &lt;- this is how Craft wants to do it
/{slug}         &lt;- this is how the client wants it done
</code></pre>

<p>I have tried to do this using routes, and can easily route to the correct template, however the <code>entry</code> is not defined.</p>

<p>Is there any easy way to not have to use Craft's routing that requires the <code>events</code> portion of the url? And have the <code>entry</code> variable available in the template?</p>

<p>Added to this is the fact I need to do URLs like</p>

<pre><code>/{slug}/blog/{article-slug}
/{slug}/faqs/{question-slug}
etc etc
</code></pre>

<p>The homepage is a static template, but as there is no content to exist under <code>/events</code>, and being that <code>events</code> is the root of all other content (which is filtered by the event slug) I can't see a way so far to not have to use the full path the 'Craft' way.</p>

<p>Bonus Question:</p>

<p>Is there any way to prevent <code>/events/{slug}</code> from responding to requests also? Is it just using the <code>exit 404</code> tag in templating?</p>

<templating><routing>
How to do advanced routing to a Craft channel
# How to do advanced routing to a Craft channel

(Asked by: username_0 on 2017-01-18)

For a project I have, I am unable to use the standard Craft routing for a channel. For example,
/events/{slug}  <- this is how Craft wants to do it
/{slug}         <- this is how the client wants it done

I have tried to do this using routes, and can easily route to the correct template, however the entry is not defined.
Is there any easy way to not have to use Craft's routing that requires the events portion of the url? And have the entry variable available in the template?
Added to this is the fact I need to do URLs like
/{slug}/blog/{article-slug}
/{slug}/faqs/{question-slug}
etc etc

The homepage is a static template, but as there is no content to exist under /events, and being that events is the root of all other content (which is filtered by the event slug) I can't see a way so far to not have to use the full path the 'Craft' way.
Bonus Question:
Is there any way to prevent /events/{slug} from responding to requests also? Is it just using the exit 404 tag in templating?

**Tags:** templating, routing

---

## 1 answers

### Answer by username_1 on 2017-01-18. Score: 5 | Accepted answer

I'm not sure I understand your question, but to try:
There actually isn't a "standard Craft routing", for channel Sections or anything else.
In your channel's Section settings (available via Settings -> Sections -> [your event section]), there's a field called Entry URL format. Simply add {slug} to that field (and nothing else), and you should be good to go. You can even use the same URL format for different sections.
In regards to your "bonus question", if you set the Entry URL format to simply {slug} for the events section, Craft will probably throw 404 for any /events/{slug} URLs, out of the box.
As for dynamic routes, you're correct that Craft won't populate your template with the entry variable – it will however, populate the template with variables matching the tokens that you add to your route. So, if you e.g. create a dynamic route that uses the  token, you can pull the entry in the template like this:
{# At the top of your template: #}

{% set entry = craft.entries.section('events').slug(slug).first() %}
{% if not entry %}
   {% exit 404 %}
{% endif %}

{# ...the rest of your template #}

Beyond the above – if you're still unsure how to set things up properly, I'd suggest taking a look at the official docs on routing, which explains in detail how Craft evaluates requests and what your different routing options are. Particularly important if you figure that you do need to do some custom routing beyond what the URL format setting can do.


---

