# Rewrite conversion

**URL:** <https://www.ruby-forum.com/t/rewrite-conversion/209810>\
**Category:** NGINX\
**Created:** [July 23, 2011, 9:08am UTC](https://www.ruby-forum.com/t/rewrite-conversion/209810 "2011-07-23T09:08:30Z")\
**Posts on this page:** 8\
**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:** [July 23, 2011, 9:08am UTC](https://www.ruby-forum.com/t/rewrite-conversion/209810/1 "2011-07-23T09:08:30Z")

</div>

Hello,

I’m trying to convert from apache to nginx rewrite.

ErrorDocument 403 /index.php?do=/public/error/403/  
ErrorDocument 404 /index.php?do=/public/error/404/

RewriteEngine On

RewriteCond %{REQUEST\_URI} !^/file/.\*  
RewriteCond %{REQUEST\_URI} !^/install/.\*  
RewriteCond %{REQUEST\_URI} !^/design/.\*  
RewriteCond %{REQUEST\_URI} !^/plugins/.\*  
RewriteRule ^index.php(/.\*)$ /index.php?do=$1 [L]

RewriteCond %{REQUEST\_URI} !^/file/.\*  
RewriteCond %{REQUEST\_URI} !^/install/.\*  
RewriteCond %{REQUEST\_URI} !^/design/.\*  
RewriteCond %{REQUEST\_URI} !^/plugins/.\*  
RewriteCond %{REQUEST\_URI} !^/index.php  
RewriteCond %{REQUEST\_URI} !^/robots.txt  
RewriteCond %{REQUEST\_URI} !^/favicon.ico  
RewriteRule ^(.\*)$ /index.php?do=/$1 [L]

This is my try 😃

error\_page 403 /index.php?do=/public/error/403/;  
error\_page 404 /index.php?do=/public/error/404/;

rewrite ^index.php(/._)$ /index.php?do=$1 last;  
rewrite ^(._)$ /index.php?do=/$1 last;

I have difficulty on REQUEST\_URI with following paths.

any help is appreciated.

Thank you.  
Tseveen.

---

<div class="post-metadata">

**Author:** ![Tseveendorj\_Ochirla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@Tseveendorj\_Ochirla](https://www.ruby-forum.com/u/Tseveendorj_Ochirla)\
**Post date:** [July 23, 2011, 9:19am UTC](https://www.ruby-forum.com/t/rewrite-conversion/209810/2 "2011-07-23T09:19:01Z")

</div>

On Sat, Jul 23, 2011 at 04:07:27PM +0900, Tseveendorj Ochirlantuu wrote:

> RewriteCond %{REQUEST\_URI} !^/install/.\*  
> RewriteCond %{REQUEST\_URI} !^/favicon.ico
> 
> I have difficulty on REQUEST\_URI with following paths.
> 
> any help is appreciated.

server {  
…

```
root /path/to/files;

error_page 403 /index.php?do=/public/error/403/;
error_page 404 /index.php?do=/public/error/404/;

location /file/ {
}

location /install/ {
}

location /design/ {
}

location /plugins/ {
}

location = /robots.txt {
}

location = /favicon.ico {
}

location / {
    fastcgi_pass ...
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /path/to/files/index.php;
    fastcgi_param QUERY_STRING do=$uri;
}

location /index.php {

    location = /index.php {
        fastcgi_pass ...
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /path/to/files/index.php;
        fastcgi_param QUERY_STRING $args;
    }

    location ~ ^/index.php(/.*)$ {
        fastcgi_pass ...
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /path/to/files/index.php;
        fastcgi_param QUERY_STRING do=$1;
    }

    return 404;
}

```

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![Tseveendorj\_Ochirla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@Tseveendorj\_Ochirla](https://www.ruby-forum.com/u/Tseveendorj_Ochirla)\
**Post date:** [July 23, 2011, 9:32am UTC](https://www.ruby-forum.com/t/rewrite-conversion/209810/3 "2011-07-23T09:32:35Z")

</div>

Dear Igor,

Thank you for your conversion. It is working now.  
I never imagine this apache rewrite would be like this.

Thank you very much again.

Tseveen.

---

<div class="post-metadata">

**Author:** ![Tseveendorj\_Ochirla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@Tseveendorj\_Ochirla](https://www.ruby-forum.com/u/Tseveendorj_Ochirla)\
**Post date:** [July 23, 2011, 4:34pm UTC](https://www.ruby-forum.com/t/rewrite-conversion/209810/4 "2011-07-23T16:34:18Z")

</div>

Dear Igor,

## I just tested rewrite but one thing did not work. When I’m accessing [http://www.xac.mn/index.php?do=/mytunes/view/song\_40/module\_popout/](http://www.xac.mn/index.php?do=/mytunes/view/song_40/module_popout/) this popup but I got 404 Not Found

nginx/0.7.65  
on the screen.Above url is working on Apache with rewrite.  
I do not know difference between these two rewrites.  
ApacheRewriteRule ^(._)$ /index.php?do=/$1 [L]  
Nginxlocation ~ ^/index.php(/._)$ { fastcgi\_pass backend;  
include fastcgi\_params; fastcgi\_param  
SCRIPT\_FILENAME  
/var/www/xac/index.php; fastcgi\_param QUERY\_STRING  
do=$1;  
}

In my opinion : On apache rewrite do=/$1 slash stand before $1 but on  
the  
Nginx do=$1 without slash before $1. Is it same or different ? Because  
above  
link does not work.  
Any help will be appreciated.  
Thank youTseveen

On Sat, Jul 23, 2011 at 7:54 PM, Tseveendorj Ochirlantuu \<

---

<div class="post-metadata">

**Author:** ![Tseveendorj\_Ochirla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@Tseveendorj\_Ochirla](https://www.ruby-forum.com/u/Tseveendorj_Ochirla)\
**Post date:** [July 23, 2011, 5:06pm UTC](https://www.ruby-forum.com/t/rewrite-conversion/209810/5 "2011-07-23T17:06:31Z")

</div>

On Sat, Jul 23, 2011 at 9:33 PM, Tseveendorj Ochirlantuu  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I do not know difference between these two rewrites.  
> Apache  
> RewriteRule ^(._)$ /index.php?do=/$1 [L]  
> Nginx location ~ ^/index.php(/._)$ { fastcgi\_pass backend;  
> include fastcgi\_params; fastcgi\_param  
> SCRIPT\_FILENAME /var/www/xac/index.php;  
> fastcgi\_param QUERY\_STRING do=$1; }

the url  
[http://www.xac.mn/index.php?do=/mytunes/view/song\_40/module\_popout/](http://www.xac.mn/index.php?do=/mytunes/view/song_40/module_popout/)  
is handled by this location block:

```
   location = /index.php {
       fastcgi_pass ...
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME /path/to/files/index.php;
       fastcgi_param QUERY_STRING $args;
   }

```

---

<div class="post-metadata">

**Author:** ![Tseveendorj\_Ochirla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@Tseveendorj\_Ochirla](https://www.ruby-forum.com/u/Tseveendorj_Ochirla)\
**Post date:** [July 25, 2011, 3:11am UTC](https://www.ruby-forum.com/t/rewrite-conversion/209810/6 "2011-07-25T03:11:31Z")

</div>

On 11.07.23 23:05, Edho A. wrote:

> > nginx/0.7.65  
> > is handled by this location block:  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

Really, I thought above rewrite handled by another. What do you think  
this popup does not work ?

---

<div class="post-metadata">

**Author:** ![Tseveendorj\_Ochirla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@Tseveendorj\_Ochirla](https://www.ruby-forum.com/u/Tseveendorj_Ochirla)\
**Post date:** [July 23, 2011, 12:55pm UTC](https://www.ruby-forum.com/t/rewrite-conversion/209810/7 "2011-07-23T12:55:25Z")

</div>

Dear Igor,

I have faced trouble on rewrite when new site installed on nginx. Most  
script has pre-configured rewrite on apache.  
Do you know what document is very useful for converting apache rewrite  
to  
nginx ? I will try learn for myself.

Thank you.  
Tseveen.

On Sat, Jul 23, 2011 at 4:31 PM, Tseveendorj Ochirlantuu \<

---

<div class="post-metadata">

**Author:** ![Tseveendorj\_Ochirla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@Tseveendorj\_Ochirla](https://www.ruby-forum.com/u/Tseveendorj_Ochirla)\
**Post date:** [November 11, 2011, 2:26pm UTC](https://www.ruby-forum.com/t/rewrite-conversion/209810/8 "2011-11-11T14:26:14Z")

</div>

Dear Igor,

You have converted apache rewrite to nginx. I appreciated very much for  
you.  
But I need redirecting request www to non-www in my rewrite. Please  
see your converted rewrite below.

Any help will be appreciated.

server {  
…

root /path/to/files;

error\_page 403 /index.php?do=/public/error/403/;  
error\_page 404 /index.php?do=/public/error/404/;

location /file/ {  
}

location /install/ {  
}

location /design/ {  
}

location /plugins/ {  
}

location = /robots.txt {  
}

location = /favicon.ico {  
}

location / {  
fastcgi\_pass …  
include fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME /path/to/files/index.php;  
fastcgi\_param QUERY\_STRING do=$uri;  
}

location /index.php {

```
   location = /index.php {
       fastcgi_pass ...
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME /path/to/files/index.php;
       fastcgi_param QUERY_STRING $args;
   }

   location ~ ^/index.php(/.*)$ {
       fastcgi_pass ...
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME /path/to/files/index.php;
       fastcgi_param QUERY_STRING do=$1;
   }

   return 404;

```

}
