<p>An easy fix could be to define an additional route without the offset parameter:</p>

<pre><code>return array(
    'articles/(?P&lt;sectionId&gt;\d+)' =&gt; 'articles/index',
    'articles/(?P&lt;sectionId&gt;\d+)/(?P&lt;offset&gt;\d+)' =&gt; 'articles/index',
);
</code></pre>

<p>In your template, you'd need to check if the offset parameter is defined, and if not, set it to 0:</p>

<pre><code>{{ offset | default( 0 ) }}
</code></pre>

<p>or</p>

<pre><code>{% set offset = offset is defined ? offset : 0 %}
</code></pre>

<p><strong>EDIT:</strong></p>

<p>Another way to solve this is by making the <em>offset</em> segment capturing group optional, as per @carlcs' <a href="https://craftcms.stackexchange.com/a/8569/1098">answer</a> to <a href="https://craftcms.stackexchange.com/questions/8568/is-it-possible-to-define-optional-route-segments/8569#8569">this question</a> – in my opinion this is a bit more elegant than having two different routes:</p>

<pre><code>return array(
    'articles/(?P&lt;sectionId&gt;\d+)(/(?P&lt;offset&gt;\d+))?' =&gt; 'articles/index',
);
</code></pre>

<p>The trick here is wrapping the last segment in parentheses, appending a question mark at the end.</p>

CC BY-SA 3.0
2015-03-07T11:43:58.437Z
Mats, probably a good workaround. Just wanted to add that there's also this ternary syntax possible: `{% set offset = offset is defined ?: 0 %}`.
carlcs
CC BY-SA 3.0
2015-03-07T12:06:21.240Z
@carlcs – Nice one, even more succinct :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-07T08:07:31.230Z
2015-03-08T10:45:08.847Z
2017-04-13T12:51:00.253Z
Mats Mikkel Rummelhoff
<p>I have the following in my routes.php file:</p>

<pre><code>return array(
    'articles/(?P&lt;sectionId&gt;\d+)/(?P&lt;offset&gt;\d+)' =&gt; 'articles/index'
);
</code></pre>

<p>And while localhost/articles/4/1, for example, routes properly to the template, localhost/articles/4/0 does not and I instead get a 404.</p>

<p>Is there an issue with zeros being picked up as valid URI segments?</p>

<routes><regex>
Route that should be matching numbers not matching a zero?
# Route that should be matching numbers not matching a zero?

(Asked by: username_0 on 2015-03-05)

I have the following in my routes.php file:
return array(
    'articles/(?P\d+)/(?P\d+)' => 'articles/index'
);

And while localhost/articles/4/1, for example, routes properly to the template, localhost/articles/4/0 does not and I instead get a 404.
Is there an issue with zeros being picked up as valid URI segments?

**Tags:** routes, regex


---

## 1 answers

### Answer by username_1 on 2015-03-07. Score: 2

An easy fix could be to define an additional route without the offset parameter:
return array(
    'articles/(?P\d+)' => 'articles/index',
    'articles/(?P\d+)/(?P\d+)' => 'articles/index',
);

In your template, you'd need to check if the offset parameter is defined, and if not, set it to 0:
{{ offset | default( 0 ) }}

or
{% set offset = offset is defined ? offset : 0 %}

EDIT:
Another way to solve this is by making the offset segment capturing group optional, as per @username_2' answer to this question – in my opinion this is a bit more elegant than having two different routes:
return array(
    'articles/(?P\d+)(/(?P\d+))?' => 'articles/index',
);

The trick here is wrapping the last segment in parentheses, appending a question mark at the end.

> **Comments:**
> **username_2**: Mats, probably a good workaround. Just wanted to add that there's also this ternary syntax possible: `{% set offset = offset is defined ?: 0 %}`.  
> **username_1**: @username_2 – Nice one, even more succinct :)  

---

