<p>Your regex is going to become more complex and potentially less specific if you try to address both scenarios in a single route as the height and width parameters match numbers and the final segment of your URL matches any characters that would be found in a URL segment.</p>

<p>It may be easier to handle this as two rules, the more specific one first:</p>

<pre><code>'/images/&lt;width:\d+&gt;/&lt;height:\d+&gt;/&lt;imageName:([-\w.]+)&gt;'
'/images/&lt;width:\d+&gt;/&lt;imageName:([-\w.]+)&gt;'
</code></pre>

<p>Making this two rules you get around needing to matching both a number OR other characters in the third segment. If the second and third segment are numbers and the fourth segment is present, your first rule will match. If the third segment is not a number and there is no fourth segment, your second rule will match.</p>

<p>Also, with two rules you can easily test if <code>height</code> exists or not in your controller or template. If you combine that third segment into a compound rule, you may need additional logic in your template to sort out what value you are working with.</p>

CC BY-SA 4.0
2018-07-05T08:16:14.387Z
Somehow i "fogot" you can target the same controller with two routes...
I think i tried to overcomplicate things. Thanks for the answer Ben!
Leonardo
CC BY-SA 4.0
2018-07-03T16:23:22.917Z
2018-07-03T16:23:22.917Z
Ben Parizek
<p>In the following route configuration i want to make the height segment optional.
So that</p>

<ul>
<li>/images/400/200/placeholder.png</li>
<li>/images/400/placeholder.png</li>
</ul>

<p>are both possible with one route.</p>

<pre><code>$event-&gt;rules['/images/&lt;width:\d+&gt;/&lt;height:\d+&gt;/&lt;imageName:([-\w.]+)&gt;'] = 'module-name/image';
</code></pre>

<p>I tried the answer from this question <a href="https://craftcms.stackexchange.com/questions/8568/is-it-possible-to-define-optional-route-segments">Is it possible to define optional route segments?</a> but couldnt bring it to work.</p>

CC BY-SA 4.0
2018-07-03T16:26:06.617Z
Note, the other question you linked to is showing an older Craft 2 syntax for routes. The example you provided in this question indicates you are using Craft 3/Yii2.
Ben Parizek
<craft3><routing>
Make segment in the middle of a route optional
# Make segment in the middle of a route optional

(Asked by: username_0 on 2018-07-03)

In the following route configuration i want to make the height segment optional.
So that

/images/400/200/placeholder.png
/images/400/placeholder.png

are both possible with one route.
$event->rules['/images///'] = 'module-name/image';

I tried the answer from this question Is it possible to define optional route segments? but couldnt bring it to work.

**Tags:** craft3, routing
> **Comments:**
> **username_1**: Note, the other question you linked to is showing an older Craft 2 syntax for routes. The example you provided in this question indicates you are using Craft 3/Yii2.  

---

## 1 answers

### Answer by username_1 on 2018-07-03. Score: 3 | Accepted answer

Your regex is going to become more complex and potentially less specific if you try to address both scenarios in a single route as the height and width parameters match numbers and the final segment of your URL matches any characters that would be found in a URL segment.
It may be easier to handle this as two rules, the more specific one first:
'/images///'
'/images//'

Making this two rules you get around needing to matching both a number OR other characters in the third segment. If the second and third segment are numbers and the fourth segment is present, your first rule will match. If the third segment is not a number and there is no fourth segment, your second rule will match.
Also, with two rules you can easily test if height exists or not in your controller or template. If you combine that third segment into a compound rule, you may need additional logic in your template to sort out what value you are working with.

> **Comments:**
> **username_0**: Somehow i "fogot" you can target the same controller with two routes... I think i tried to overcomplicate things. Thanks for the answer Ben!  

---

