# Rewrite rule and the try\_files directive

**URL:** <https://www.ruby-forum.com/t/rewrite-rule-and-the-try-files-directive/220046>\
**Category:** NGINX\
**Created:** [May 25, 2012, 2:01pm UTC](https://www.ruby-forum.com/t/rewrite-rule-and-the-try-files-directive/220046 "2012-05-25T14:01:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurdipe\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Gurdipe\_D](https://www.ruby-forum.com/u/Gurdipe_D)\
**Post date:** [May 25, 2012, 2:01pm UTC](https://www.ruby-forum.com/t/rewrite-rule-and-the-try-files-directive/220046/1 "2012-05-25T14:01:42Z")

</div>

Hi guys,

I’m a little stuck here and wondered if anyone could help, I have a  
customer’s website with the following rewrite rule:

‘rewrite ^/(.\*)$ /index.php?town=$1 last;’

This has functioned just fine until they’ve added three smaller  
management sites that they want to be able to reach by appending either  
/mms, /admin or /newsletter to the end of the URL (I have tried to steer  
them towards subdomains but to no avail!) now these are obviously being  
picked up by my rewrite rule and as the website doesn’t recognise these  
as “towns” it’s simply redirecting them back to the home page.

Does anyone know a clean way of achieving this, I was originally looking  
at an ‘if’ statement but I’ve been warned off this when it comes to  
nginx due to the obvious reasons, I’ve been looking in to the try\_files  
directive but I’m still a little unsure as to how it actually works.

Any help greatly appreciated.

Thanks,

Andy

Posted at Nginx Forum:

> **[Rewrite rule and the try\_files directive](https://forum.nginx.org/read.php?2,226772,226772#msg-226772)**
>
> Hi guys, I'm a little stuck here and wondered if anyone could help, I have a customer's website with the following rewrite rule: 'rewrite ^/(.\*)$ /index.php?town=$1 last;' This has functioned just fine until they've added three smaller management...

---

<div class="post-metadata">

**Author:** ![Captain\_Quor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@Captain\_Quor](https://www.ruby-forum.com/u/Captain_Quor)\
**Post date:** [May 25, 2012, 3:01pm UTC](https://www.ruby-forum.com/t/rewrite-rule-and-the-try-files-directive/220046/2 "2012-05-25T15:01:48Z")

</div>

On Fri, May 25, 2012 at 7:01 PM, Captain\_Quor [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> them towards subdomains but to no avail!) now these are obviously being  
> picked up by my rewrite rule and as the website doesn’t recognise these  
> as “towns” it’s simply redirecting them back to the home page.
> 
> Does anyone know a clean way of achieving this, I was originally looking  
> at an ‘if’ statement but I’ve been warned off this when it comes to  
> nginx due to the obvious reasons, I’ve been looking in to the try\_files  
> directive but I’m still a little unsure as to how it actually works.

location /admin/ {  
…  
}  
location /mms/ {  
…  
}  
location /newsletter/ {  
…  
}  
location / {  
rewrite …  
}
