# Regex rewrite to root document

**URL:** <https://www.ruby-forum.com/t/regex-rewrite-to-root-document/215783>\
**Category:** NGINX\
**Created:** [January 16, 2012, 5:35am UTC](https://www.ruby-forum.com/t/regex-rewrite-to-root-document/215783 "2012-01-16T05:35:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 16, 2012, 5:35am UTC](https://www.ruby-forum.com/t/regex-rewrite-to-root-document/215783/1 "2012-01-16T05:35:58Z")

</div>

I have a simple re-write of

rewrite ^/(.\*) /post.php?id=$1 last;

This will breaks my main index page, as [domain.com](http://domain.com) will also be  
redirected to “post.php?id=”

How can I modify my regular expression not to change the main index  
page?

Posted at Nginx Forum:

> **[Regex rewrite to root document](https://forum.nginx.org/read.php?2,221278,221278#msg-221278)**
>
> I have a simple re-write of rewrite ^/(.\*) /post.php?id=$1 last; This will breaks my main index page, as domain.com will also be redirected to "post.php?id=" How can I modify my regular expression not to change the main index page?

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [January 16, 2012, 5:59am UTC](https://www.ruby-forum.com/t/regex-rewrite-to-root-document/215783/2 "2012-01-16T05:59:25Z")

</div>

On 16 Jan 2012 04h35 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a simple re-write of
> 
> rewrite ^/(.\*) /post.php?id=$1 last;
> 
> This will breaks my main index page, as [domain.com](http://domain.com) will also be  
> redirected to “post.php?id=”
> 
> How can I modify my regular expression not to change the main index  
> page?

Easier to define _another_ location without any rewrite just for / :

location = / {  
…  
}

location / {  
# do the rewrite here  
…  
}

— appa

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [January 16, 2012, 10:58am UTC](https://www.ruby-forum.com/t/regex-rewrite-to-root-document/215783/3 "2012-01-16T10:58:42Z")

</div>

On Sun, Jan 15, 2012 at 11:35:44PM -0500, etrader wrote:

> I have a simple re-write of
> 
> rewrite ^/(.\*) /post.php?id=$1 last;
> 
> This will breaks my main index page, as [domain.com](http://domain.com) will also be  
> redirected to “post.php?id=”

location = / {  
…  
}

location ~ ^/(?.+)$ {  
fastcgi\_pass …  
include fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME /path/to/post/php;  
fastcgi\_param QUERY\_STRING id=$id;  
}

–  
Igor S.
