# Simple rewrite rules

**URL:** <https://www.ruby-forum.com/t/simple-rewrite-rules/207262>\
**Category:** NGINX\
**Created:** [May 22, 2011, 2:42am UTC](https://www.ruby-forum.com/t/simple-rewrite-rules/207262 "2011-05-22T02:42:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 22, 2011, 2:42am UTC](https://www.ruby-forum.com/t/simple-rewrite-rules/207262/1 "2011-05-22T02:42:39Z")

</div>

Hello everyone,  
I’m trying to setup some basic rewrite rules in nginx (I’m sorry if I  
may be  
asking obvious question but I moved my servers from apache to nginx and  
I  
can’t figure out how to write simple rewrite rules in nginx).

This is what I am trying to achieve:

I have a URL with the following arguments:

[http://www.domain.com/browse/category](http://www.domain.com/browse/category)

What I want to achieve is:

- When someone try to reach [http://www.domain.com/category](http://www.domain.com/category) - it will  
serve  
the url [http://www.domain.com/browse/category](http://www.domain.com/browse/category) internally (no 301  
redirect).
- When someone will try to reach [http://www.domain.com/browse/category](http://www.domain.com/browse/category)  
it  
will redirect externally (301) to [http://www.domain.com/category](http://www.domain.com/category)

I’ve added 2 rewriterule at first but somehow only the first rule works,  
the  
second won’t work:

```
    rewrite ^/browse/(.*) http://www.domain.com/$1 permanent;
    rewrite ^/(category1|category2|category3|category4)$ 

```

/browse/$1  
last;

Thanks (and sorry for asking a somewhat newbie question!)

p.s: yes, I’ve read this [Module ngx\_http\_rewrite\_module](http://wiki.nginx.org/HttpRewriteModule)

---

<div class="post-metadata">

**Author:** ![Adam\_Benayoun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@Adam\_Benayoun](https://www.ruby-forum.com/u/Adam_Benayoun)\
**Post date:** [May 22, 2011, 7:22am UTC](https://www.ruby-forum.com/t/simple-rewrite-rules/207262/2 "2011-05-22T07:22:19Z")

</div>

On Sun, May 22, 2011 at 03:41:59AM +0300, Adam Benayoun wrote:

> ```
> rewrite ^/(category1|category2|category3|category4)$ /browse/$1
> 
> ```
> 
> last;
> 
> Thanks (and sorry for asking a somewhat newbie question!)
> 
> p.s: yes, I’ve read this [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpRewriteModule)

You should not read anyting about rewrite module. It’s better to forget  
about it. You should think using locations.

```
 location /category1/
     #depending on processing method:

     # FastCGI
     fastcgi_pass backend;
     fatscgi_param SCRIPT_FILENMAE /path/to/browse;

     # proxying
     proxy_pass http://backend/browse/

     # static files
     root /path/to/browse;
 }

 location /browse/ {
     rewrite ^/browse/(.*) /$1 permanent;
 }

```

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![Adam\_Benayoun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@Adam\_Benayoun](https://www.ruby-forum.com/u/Adam_Benayoun)\
**Post date:** [May 22, 2011, 9:51am UTC](https://www.ruby-forum.com/t/simple-rewrite-rules/207262/3 "2011-05-22T09:51:32Z")

</div>

Igor,  
Thanks for the pointer however there is one last thing I’m not sure how  
to  
do.  
Basically my application is built on top of a framework meaning that  
everything should go through index.php and then the routers configured  
appropriately should serve you different templates and controllers based  
on  
the REQUEST\_URI.

Basically when I try to access [http://www.domain.com/category](http://www.domain.com/category) - it  
should  
pass to index.php the request\_uri /browse/category and then serve the  
appropriate output.  
This is how my config look like, let me know if that make sense and what  
should I add to mimic that request\_uri through index.php

Cheers,

server {  
listen \*:80;  
server\_name [www.domain.com](http://www.domain.com);  
access\_log /var/www/domain/logs/access\_log main;  
error\_log /var/www/domain/logs/error\_log debug;  
root /var/www/domain/html/http;  
index index.php;

```
    if ($http_host !~ "^www\.domain\.com$") {
            rewrite ^(.*) http://www.domain.com$1 permanent;
    }

    location / {
            try_files $uri $uri/ /index.php?$args;
    }
    location /browse {
            #rewrite ^/browse/(.*) http://www.domain.com/$1 

```

permanent;  
}  
location /min {  
try\_files $uri $uri/ /min/index.php?q=;  
}  
location /blog {  
try\_files $uri $uri/ /blog/index.php?q=$1;  
}  
location /apc {  
try\_files $uri $uri/ /apc.php$args;  
}

```
    location ~ \.php {
            include /etc/nginx/fastcgi_params;
            fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;  
fastcgi\_param PATH\_INFO $fastcgi\_script\_name;  
fastcgi\_param SERVER\_NAME $http\_host;  
fastcgi\_pass 127.0.0.1:9000;  
}

```
    location ~* ^.+\.(ht|svn)$ {
            deny all;
    }

    # Static files location
    location ~*

```

^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$  
{  
expires max;  
}  
}
