# Url rewrite for restful url

**URL:** <https://www.ruby-forum.com/t/url-rewrite-for-restful-url/222001>\
**Category:** NGINX\
**Created:** [August 8, 2012, 8:30pm UTC](https://www.ruby-forum.com/t/url-rewrite-for-restful-url/222001 "2012-08-08T20:30:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [August 8, 2012, 8:30pm UTC](https://www.ruby-forum.com/t/url-rewrite-for-restful-url/222001/1 "2012-08-08T20:30:57Z")

</div>

I have a Rails web app which has static files under dynamic restful  
urls. For example:

/projects/1/attachments/some\_file.xls

I want to setup Nginx to redirect to the static file on the server:

/public/attachments/1/some\_file.xls

Where 1 is the dynamic project id.

How would the location block and rewrite statement look for the Nginx  
config file?

StackOverflow link:

> <https://stackoverflow.com/questions/11852854/nginx-url-rewrite-for-restful-url>

---

<div class="post-metadata">

**Author:** ![ejstembler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b9e5f3/32.png) [@ejstembler](https://www.ruby-forum.com/u/ejstembler)\
**Post date:** [August 9, 2012, 1:58am UTC](https://www.ruby-forum.com/t/url-rewrite-for-restful-url/222001/2 "2012-08-09T01:58:17Z")

</div>

On Wed, Aug 08, 2012 at 08:30:57PM +0200, Edward S. wrote:

Hi there,

> /projects/1/attachments/some\_file.xls
> 
> I want to setup Nginx to redirect to the static file on the server:
> 
> /public/attachments/1/some\_file.xls
> 
> Where 1 is the dynamic project id.

If you want requests like /projects/N/attachments/F to return the  
content  
of the file /public/attachments/N/F, then “alias” is probably the way  
to go.

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#alias)**
>
> This directive appeared in version 1.11.8.

location ~ ^/projects/(\d+)/attachments/(.\*) {  
alias /public/attachments/$1/$2;  
}

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
