Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-07T11:43:58.437Z
Answers.Text: Mats, probably a good workaround. Just wanted to add that there's also this ternary syntax possible: `{% set offset = offset is defined ?: 0 %}`.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-07T12:06:21.240Z
Answers.Text: @carlcs – Nice one, even more succinct :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-07T08:07:31.230Z
Answers.LastActivityDate: 2015-03-08T10:45:08.847Z
Answers.LastEditDate: 2017-04-13T12:51:00.253Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <routes><regex>
Title: Route that should be matching numbers not matching a zero?
ThreadText: # 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<sectionId>\d+)/(?P<offset>\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<sectionId>\d+)' => 'articles/index',
      'articles/(?P<sectionId>\d+)/(?P<offset>\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](https://craftcms.stackexchange.com/a/8569/1098) to [this question](https://craftcms.stackexchange.com/questions/8568/is-it-possible-to-define-optional-route-segments/8569#8569) – in my opinion this is a bit more elegant than having two different routes:
  
  ```
  return array(
      'articles/(?P<sectionId>\d+)(/(?P<offset>\d+))?' => 'articles/index',
  );
  
  ```
  
  The trick here is wrapping the last segment in parentheses, appending a question mark at the end.
  
  > **Comments:**
  > **username_2**: username_1, 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 :)  
  
  ---
  
  