# Help translating Apache RewriteRules

**URL:** <https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442>\
**Category:** NGINX\
**Created:** [April 7, 2010, 9:26am UTC](https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442 "2010-04-07T09:26:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Cortese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@Chris\_Cortese](https://www.ruby-forum.com/u/Chris_Cortese)\
**Post date:** [April 7, 2010, 9:26am UTC](https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442/1 "2010-04-07T09:26:41Z")

</div>

Hello,

I’ve been using nginx for 1.5 years now with about 10 or so CodeIgniter  
sites, but apparently I still haven’t mastered the rewrite stuff.

With the CodeIgniter stuff (and Igor’s help), I never had to use the  
“rewrite” command at all.

Anyway, now I need to translate the following (from .htaccess for  
Apache):

RewriteRule ^([0-9a-zA-Z/-\_@]+)$ index.php?demand=$1 [QSA,L]

My current conf file is going into a loop and eventually returning  
nothing:

server {  
listen 80;  
server\_name mysite.com.vmware;

access\_log /usr/local/nginx/logs/mysite.com.access.log;  
error\_log /usr/local/nginx/logs/mysite.com.error.log;

root /home/mylinuxuser/www/live/mysite.com;  
index index.php;

rewrite ^/([0-9a-zA-Z/-\_@]+)$ /index.php?demand=$1 last;

location / {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_param SCRIPT\_FILENAME  
/home/mylinuxuser/www/live/mysite.com/index.php;  
include /usr/local/nginx/conf/fastcgi\_params;  
}  
}

* * *

Can someone please advise?

Thanks!  
Chris

---

<div class="post-metadata">

**Author:** ![Chris\_Cortese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@Chris\_Cortese](https://www.ruby-forum.com/u/Chris_Cortese)\
**Post date:** [April 7, 2010, 10:05am UTC](https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442/2 "2010-04-07T10:05:54Z")

</div>

On Wed, Apr 07, 2010 at 12:25:37AM -0700, Chris Cortese wrote:

> RewriteRule ^([0-9a-zA-Z/-\_@]+)$ index.php?demand=$1 [QSA,L]  
> root /home/mylinuxuser/www/live/mysite.com;  
> }
> 
> * * *
> 
> Can someone please advise?

```
location ~ ^/([0-9a-zA-Z\/\-\_\@]+)$ {
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_param SCRIPT_FILENAME
                /home/mylinuxuser/www/live/mysite.com/index.php;
  fastcgi_param QUERY_STRING demand=$uri;
  include /usr/local/nginx/conf/fastcgi_params0;
}

location / {
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_param SCRIPT_FILENAME
                /home/mylinuxuser/www/live/mysite.com/index.php;
  include /usr/local/nginx/conf/fastcgi_params;
}

```

fastcgi\_params0 is copy of fastcgi\_params without QUERY\_STRING  
parameter.

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![Chris\_Cortese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@Chris\_Cortese](https://www.ruby-forum.com/u/Chris_Cortese)\
**Post date:** [April 7, 2010, 10:07am UTC](https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442/3 "2010-04-07T10:07:40Z")

</div>

On Wed, Apr 07, 2010 at 12:05:07PM +0400, Igor S. wrote:

> > Anyway, now I need to translate the following (from .htaccess for Apache):  
> > error\_log /usr/local/nginx/logs/mysite.com.error.log;  
> > include /usr/local/nginx/conf/fastcgi\_params;  
> > /home/mylinuxuser/www/live/mysite.com/index.php;  
> > fastcgi\_param QUERY\_STRING demand=$uri;

A typo:

- 

```
  fastcgi_param QUERY_STRING demand=$uri;

```

- 

```
  fastcgi_param QUERY_STRING demand=$1;

```

> fastcgi\_params0 is copy of fastcgi\_params without QUERY\_STRING parameter.
> 
> –  
> Igor S.  
> [Igor Sysoev](http://sysoev.ru/en/)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![Chris\_Cortese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@Chris\_Cortese](https://www.ruby-forum.com/u/Chris_Cortese)\
**Post date:** [April 7, 2010, 10:24am UTC](https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442/4 "2010-04-07T10:24:03Z")

</div>

Thanks for the quick response Igor.

Unfortunately I’m still getting stuck in a loop.

This code which has been handed to me is using both the .htaccess rule  
(shown below) and once it hits index.php (actually an include included  
from index.php) it’s also calling a PHP function named rewrite().

Maybe I can do some cleanup later but for now I really just want to be  
able to run the site in my dev environment without having to install  
Apache…

If the nginx rewrite rules will do what the one Apache rewrite rule  
does, it really shouldn’t matter what this following code does, I  
wouldn’t think.

```auto
/* rewrite url function */
rewrite();
if (isset($_GET['selectLanguage']))
{
  if ($_GET['module'])
  {
    $reloadURL = $_GET['module'];
  }
  foreach ($_GET as $id=>$val)
  {
    if ($id != 'selectLanguage' && $id != 'module')
    {
      $reloadURL .= "/$id/$val";
    }
  }
  header('Location: '.WWW_ROOT.$reloadURL);
}
function rewrite(){

         / **URL rewriting action** /
    global $_module;
         if(isset($_GET['demand']) && $_GET['demand']!="") {

                 $_action_vars = explode('/', $_GET['demand']);

                 $_module = $_GET['module'] = trim($_action_vars[0]);

                 if(count($_action_vars) > 1) {
                         for ($i = 1; $i < count($_action_vars); $i += 
2) {
                                 if($_action_vars[$i] != ''){
                                         if (isset($_action_vars[$i+1])) 
{

$_GET[trim($_action_vars[$i])] = trim($_action_vars[$i+1]);
                                         } else {

$_GET[trim($_action_vars[$i])] = '';
                                         }
                                 }
                         }
                 }
         } else {
                 // Setting default values for null request
                 $_module = $_GET['module'] = 'index';
         }
         unset($_GET['demand']);

         foreach($_GET as $key => $val){
                 if($key == '' || $key == null)
                         unset($_GET[$key]);
         }

         / **End of URL rewriting action** /
}
```

---

<div class="post-metadata">

**Author:** ![Chris\_Cortese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@Chris\_Cortese](https://www.ruby-forum.com/u/Chris_Cortese)\
**Post date:** [April 7, 2010, 10:24am UTC](https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442/5 "2010-04-07T10:24:39Z")

</div>

ok, sent my last reply before I saw this. Let me try it now.

---

<div class="post-metadata">

**Author:** ![Chris\_Cortese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@Chris\_Cortese](https://www.ruby-forum.com/u/Chris_Cortese)\
**Post date:** [April 8, 2010, 11:06am UTC](https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442/6 "2010-04-08T11:06:56Z")

</div>

On Thu, Apr 8, 2010 at 3:44 PM, Chris Cortese  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [/code]
> 
> #RewriteRule ^(.\*)$ index.php?demand=$1 [NC,L]

simple way:

server {  
listen 80;  
server\_name mysite.com.vmware;

access\_log /usr/local/nginx/logs/mysite.com.access.log;  
error\_log /usr/local/nginx/logs/mysite.com.error.log;

root /home/mylinuxuser/www/live/mysite.com;  
index index.php;

location ~ ^/[0-9a-zA-Z/-_@]+$ {  
rewrite ^/([0-9a-zA-Z/-_@]+)$ /index.php?demand=$1 last;  
}  
location ~ .php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_param SCRIPT\_FILENAME $request\_filename;  
include /usr/local/nginx/conf/fastcgi\_params;  
}  
}

faster way:

server {  
listen 80;  
server\_name mysite.com.vmware;

access\_log /usr/local/nginx/logs/mysite.com.access.log;  
error\_log /usr/local/nginx/logs/mysite.com.error.log;

root /home/mylinuxuser/www/live/mysite.com;  
index index.php;

location ~ ^/[0-9a-zA-Z/-\_@]+)$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_param SCRIPT\_FILENAME  
/home/mylinuxuser/www/live/domain.com/index.php;  
fastcgi\_param QUERY\_STRING demand=$1;  
include /usr/local/nginx/conf/fastcgi\_params0;  
}  
location ~ .php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_param SCRIPT\_FILENAME $request\_filename;  
include /usr/local/nginx/conf/fastcgi\_params;  
}  
}

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![Chris\_Cortese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@Chris\_Cortese](https://www.ruby-forum.com/u/Chris_Cortese)\
**Post date:** [April 8, 2010, 10:44am UTC](https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442/7 "2010-04-08T10:44:41Z")

</div>

I’m still stumped on this one. Can anyone tell why I’m getting an  
endless loop? Firefox gives:

```auto
The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this
address in a way that will never complete.
   * This problem can sometimes be caused by disabling or refusing to
accept
           cookies.
```

Machine:

```auto
. Ubuntu Karmic Koala fully-upgraded/updated, VM running on VMWare
Workstation 6.5 running on Windows XP
. nginx 0.8.35 built from source
. php-5.3.2 with php-fpm
```

Working .htaccess Apache file:

```auto
Options -Indexes

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z\/\-\_\@]+)$ index.php?demand=$1 [QSA,L]
#RewriteRule ^(.*)$ index.php?demand=$1 [NC,L]

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
```

My Nginx conf file:

```auto
server {
    listen 80;
   server_name domain.com.vmware;

   access_log /usr/local/nginx/logs/domain.com.access.log;
   error_log /usr/local/nginx/logs/domain.com.error.log debug;

   root /home/mylinuxuser/www/live/domain.com;
   index index.php;

   location / {
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_param SCRIPT_FILENAME
/home/mylinuxuser/www/live/domain.com/index.php;
     include /usr/local/nginx/conf/fastcgi_params;
   }

   location ~ ^/([0-9a-zA-Z\/\-\_\@]+)$ {
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_param SCRIPT_FILENAME
/home/mylinuxuser/www/live/domain.com/index.php;
     fastcgi_param QUERY_STRING demand=$1;
     include /usr/local/nginx/conf/fastcgi_params0;
   }
}
```

Here is the 5200-line debug output from the error log when only  
requesting [http://domain.com](http://domain.com)

[http://pastebin.com/6PLyC3r8](http://pastebin.com/6PLyC3r8)

Note: fastcgi\_params is unmodified from nginx distribution.  
fastcgi\_params0 is identical except it lacks QUERY\_STRING setting

Note2: I didn’t write the web code. There is PHP code doing a  
header(“Location:… etc”) call, in order to make clean URLs. Still, if  
the PHP code works with the Apache .htaccess, it must be possible to  
have it work with Nginx configuration.

Can anyone help?

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![Chris\_Cortese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@Chris\_Cortese](https://www.ruby-forum.com/u/Chris_Cortese)\
**Post date:** [April 8, 2010, 3:25pm UTC](https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442/8 "2010-04-08T15:25:44Z")

</div>

On Thu, Apr 8, 2010 at 6:04 PM, Chris Cortese  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thanks, but still no luck, still looping.

which version did you try?

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![Chris\_Cortese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@Chris\_Cortese](https://www.ruby-forum.com/u/Chris_Cortese)\
**Post date:** [April 8, 2010, 1:04pm UTC](https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442/9 "2010-04-08T13:04:36Z")

</div>

thanks, but still no luck, still looping.

What about this part? What is the nginx equivalent of this? Where is  
there documentation on this?

```auto
<Directory "/home/mylinuxuser/www/live/domain.com">
                 allow from all
                 Options -Indexes FollowSymLinks ExecCGI
                 AllowOverride All
         </Directory>
         <Directory "/home/mylinuxuser/www/live/domain.com">
                 Options -Indexes ExecCGI FollowSymLinks
         </Directory>

```

Meanwhile I’m going to have to resort to xdebug maybe, and/or I’m also  
getting my RHEL5 VM w/Apache into shape … to also run via xdebug I  
guess… incredible… all for this one rewriterule. ☹

Chris

---

<div class="post-metadata">

**Author:** ![Chris\_Cortese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@Chris\_Cortese](https://www.ruby-forum.com/u/Chris_Cortese)\
**Post date:** [April 8, 2010, 8:20pm UTC](https://www.ruby-forum.com/t/help-translating-apache-rewriterules/186442/10 "2010-04-08T20:20:15Z")

</div>

I tried both versions.
