<blockquote>
  <p>Is it simply not possible to use backrefs in the target route templates</p>
</blockquote>

<p>Right now it's not possible (although might be worth a <a href="https://feedback.craftcms.com" rel="nofollow">feature request</a>).</p>

<p>The right hand side of the route <code>key =&gt; value</code> pair can currently only be a <a href="https://craftcms.com/docs/routing#basic-syntax" rel="nofollow">static string</a> that is a path to the template to load or an array that defines a <a href="https://craftcms.com/docs/routing#routing-to-controller-actions" rel="nofollow">controller action</a> to load.</p>

CC BY-SA 3.0
2016-10-12T14:35:44.557Z
Thanks @Brad! Assumed so but wanted to be sure it just didn't require a different syntax. And I'll def add a feature request.
Shane Robinson
CC BY-SA 3.0
2016-10-11T21:25:07.243Z
2016-10-11T21:25:07.243Z
Brad Bell
<p>I've searched and searched for info on regex backreferences in Craft's route.php file to no avail. </p>

<p>I have a URL structure required by the client along the lines of:</p>

<p>/ [state] / [section] / region / [region]</p>

<p>so for examples:</p>

<pre>
/ hawaii / hotels / region / oahu
/ hawaii / retaurants / region / oahu
/ hawaii / hotels / region / maui
</pre>

<p>The "hotels" and "restaurants" are each their own Section (Channels) and require their own templates. </p>

<p>Given the above URL examples, I've created template directories: </p>

<pre>
/hotels/region.html
/restaurants/region.html
</pre>

<p>I can manage the routes if I spell them all out. But what I'd like to do is use regex backreferences in the target templates. 
Tried but it fails. tried something like the following: </p>

<pre>
'hawaii/(.*)/region' => '\1/region'
</pre>

<p>The '\1' backreference doesn't catch the first group of either "hotels" or "restaurants." </p>

<p>Is it simply not possible to use backrefs in the target route templates...or is my syntax not correct for Craft?</p>

<templating><routes><regex>
Possible to use REGEX Backreference in Routes
# Possible to use REGEX Backreference in Routes

(Asked by: username_0 on 2016-10-11)

I've searched and searched for info on regex backreferences in Craft's route.php file to no avail. 
I have a URL structure required by the client along the lines of:
/ [state] / [section] / region / [region]
so for examples:

/ hawaii / hotels / region / oahu
/ hawaii / retaurants / region / oahu
/ hawaii / hotels / region / maui

The "hotels" and "restaurants" are each their own Section (Channels) and require their own templates. 
Given the above URL examples, I've created template directories: 

/hotels/region.html
/restaurants/region.html

I can manage the routes if I spell them all out. But what I'd like to do is use regex backreferences in the target templates. 
Tried but it fails. tried something like the following: 

'hawaii/(.*)/region' => '\1/region'

The '\1' backreference doesn't catch the first group of either "hotels" or "restaurants." 
Is it simply not possible to use backrefs in the target route templates...or is my syntax not correct for Craft?

**Tags:** templating, routes, regex


---

## 1 answers

### Answer by username_1 on 2016-10-11. Score: 1 | Accepted answer

> Is it simply not possible to use backrefs in the target route templates

Right now it's not possible (although might be worth a [feature request](https://feedback.craftcms.com)).

The right hand side of the route `key => value` pair can currently only be a [static string](https://craftcms.com/docs/routing#basic-syntax) that is a path to the template to load or an array that defines a [controller action](https://craftcms.com/docs/routing#routing-to-controller-actions) to load.

> **Comments:**
> **username_0**: Thanks @Brad! Assumed so but wanted to be sure it just didn't require a different syntax. And I'll def add a feature request.  

---

