# Quick question

**URL:** <https://www.ruby-forum.com/t/quick-question/163268>\
**Category:** NGINX\
**Created:** [March 18, 2009, 8:15pm UTC](https://www.ruby-forum.com/t/quick-question/163268 "2009-03-18T20:15:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [March 18, 2009, 8:15pm UTC](https://www.ruby-forum.com/t/quick-question/163268/1 "2009-03-18T20:15:59Z")

</div>

rewrites happen before location {} blocks are considered (if the  
rewrite is on a global level) correct?

so

location /foo {  
this will never be computed  
}

rewrite /foo/bar /somethingelse permanent;

That’s at least what it seems to me, unless I’m missing something.

It doesn’t seem like ^~ ~\* etc seem to matter either. Or is it all  
depending on the order in the config file?

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [March 18, 2009, 8:21pm UTC](https://www.ruby-forum.com/t/quick-question/163268/2 "2009-03-18T20:21:17Z")

</div>

On Wed, Mar 18, 2009 at 12:06:36PM -0700, mike wrote:

> That’s at least what it seems to me, unless I’m missing something.
> 
> It doesn’t seem like ^~ ~\* etc seem to matter either. Or is it all  
> depending on the order in the config file?

Yes, as in Apache rewrites on server level run before location search.

Then (_) nginx chooses a location, then nginx runs rewrites inside  
the location. If any rewrite is matched and it has not “break” flags,  
then nginx chooses a location again, i.e, goes to (_).  
This cycle may run no more than 10 times.

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [March 18, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/quick-question/163268/3 "2009-03-18T20:40:41Z")

</div>

well, i did it this way, and it seems to work…

rewrite ^/sites/foo/index.php$ [http://foo.com/](http://foo.com/) permanent;  
rewrite ^/sites/coolsw(|/)$ [http://foo.com/](http://foo.com/) permanent;

# kill it from search engines

rewrite ^/sites/foo/(.\*)$ /kill last;  
location /kill {  
return 404;  
}

2009/3/18 Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [March 18, 2009, 8:56pm UTC](https://www.ruby-forum.com/t/quick-question/163268/4 "2009-03-18T20:56:31Z")

</div>

2009/3/18 Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> However, it’s better to runs these rewrites under
> 
> Â Â Â location /sites/foo/ {  
> Â Â Â Â Â rewrite Â ^/sites/foo/index.php$ [http://foo.com/](http://foo.com/) permanent;  
> Â Â Â Â Â return Â 404;  
> Â Â Â }
> 
> Â Â Â location /sites/coolsw {  
> Â Â Â Â Â rewrite Â ^/sites/coolsw(|/)$ [http://foo.com/](http://foo.com/) permanent;  
> Â Â Â }

ahh, duh. this works perfect. i had another rewrite i had missed that  
was messing things up:

location /sites/foo {  
rewrite ^/sites/foo/index.php$ [http://foo.com/permanent](http://foo.com/permanent);  
rewrite ^/sites/foo(|/)$ [http://foo.com/permanent](http://foo.com/permanent);  
return 404;  
}

Thanks!

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [March 18, 2009, 8:45pm UTC](https://www.ruby-forum.com/t/quick-question/163268/5 "2009-03-18T20:45:57Z")

</div>

On Wed, Mar 18, 2009 at 12:33:15PM -0700, mike wrote:

> well, i did it this way, and it seems to work…
> 
> rewrite ^/sites/foo/index.php$ [http://foo.com/](http://foo.com/) permanent;  
> rewrite ^/sites/coolsw(|/)$ [http://foo.com/](http://foo.com/) permanent;
> 
> # kill it from search engines
> 
> rewrite ^/sites/foo/(.\*)$ /kill last;  
> location /kill {  
> return 404;  
> }

However, it’s better to runs these rewrites under

```
  location /sites/foo/ {
       rewrite ^/sites/foo/index.php$ http://foo.com/ permanent;
       return 404;
  }

  location /sites/coolsw {
       rewrite ^/sites/coolsw(|/)$ http://foo.com/ permanent;
  }

```
