# Apache rewrite in Nginx format

**URL:** <https://www.ruby-forum.com/t/apache-rewrite-in-nginx-format/115551>\
**Category:** NGINX\
**Created:** [September 26, 2007, 6:26am UTC](https://www.ruby-forum.com/t/apache-rewrite-in-nginx-format/115551 "2007-09-26T06:26:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philip\_Murray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Philip\_Murray](https://www.ruby-forum.com/u/Philip_Murray)\
**Post date:** [September 26, 2007, 6:26am UTC](https://www.ruby-forum.com/t/apache-rewrite-in-nginx-format/115551/1 "2007-09-26T06:26:32Z")

</div>

Hi,

Is it possible to express the following Apache rewrite rule in Nginx?

```
 RewriteCond %{QUERY_STRING} !^nobranding$
 RewriteCond %{REQUEST_FILENAME} ^/assets/XL/([^.]+)\.([jpegif]+)$
 RewriteCond /usr/local/www/assets-nz/XL/%1.branded.%2 -f
 RewriteRule ^/([^.]+)\.([jpeg]+)$ /$1.branded.$2

```

In Nginx, this would be in a location block with an alias, so

location /assets {  
alias /usr/local/www/assets-nz/;

```
 ... rewrites go here ...

```

}

But, I understand you can’t rewrite with an alias present? So how can  
I implement the above Apache rewrite?

Cheers

Phil Murray

---

<div class="post-metadata">

**Author:** ![Philip\_Murray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Philip\_Murray](https://www.ruby-forum.com/u/Philip_Murray)\
**Post date:** [September 26, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/apache-rewrite-in-nginx-format/115551/2 "2007-09-26T13:23:15Z")

</div>

On Wed, Sep 26, 2007 at 04:18:43PM +1200, Philip Murray wrote:

> ```
> alias /usr/local/www/assets-nz/;
> 
> ... rewrites go here ...
> 
> ```
> 
> }
> 
> But, I understand you can’t rewrite with an alias present? So how can  
> I implement the above Apache rewrite?

Try

location /assets {

```
  if ($args = nobranding) {
      rewrite ^(.+)$ /nobranding$1 last;
  }

  rewrite ^/assets/XL/(.+)\.(jpeg|jpg|gif) /XL/$1.branded.$2;
  root /usr/local/www/assets-nz;
  error_page 404 = /nobranding/assets$uri;

```

}

location /nobranding/assets {  
internal;  
alias /usr/local/www/assets-nz/;  
}

---

<div class="post-metadata">

**Author:** ![Philip\_Murray](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@Philip\_Murray](https://www.ruby-forum.com/u/Philip_Murray)\
**Post date:** [September 27, 2007, 4:15am UTC](https://www.ruby-forum.com/t/apache-rewrite-in-nginx-format/115551/3 "2007-09-27T04:15:13Z")

</div>

On 26/09/2007, at 11:14 PM, Igor S. wrote:

> > 
> 
> location /nobranding/assets {  
> internal;  
> alias /usr/local/www/assets-nz/;  
> }

Thanks Igor, that works… but to throw a spanner into the works, I  
have at the end of this configuration:

location / {  
root /path/to/rails/public/

# … lots of rewrites for fixing up old URLs

if (!-f $request\_filename) {  
proxy\_pass [http://mongrel](http://mongrel);  
break;  
}  
}

And the !-f/proxy\_pass seems to pick up the request in the case where  
blah.branded.jpeg doesn’t exist (and should just serve blah.jpeg  
instead), and thus passing it through to mongrel.

Is there a way to force the /nobranding/assets location to be used.  
Do rewrites alter $uri ?

Cheers

Phil
