Idrizi.A
Idrizi.A

Reputation: 12010

Converting a rewrite from htaccess to nginx

I'm currently using this htaccess code in Apache to change the URL in address bar from http://www.domain.com/list?m=100 to http://www.domain.com/list/100

RewriteEngine On

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-d 
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^([^.]*)$ index.php?m=$1 [QSA,L]
</IfModule>

The above htaccess is on /list directory.

I have tried to convert it to nginx (using http://winginx.com/en/htaccess), but I couldn't make it work. This is what I tried

location /list/ {
    if (!-e $request_filename){
        rewrite ^/([^.]*)$ /index.php?m=$1 break;
    }
}

The above just downloads the PHP code. I also tried changing the break to last, but it just opens the homepage (the url in the address bar changes to http://www.domain.com/list/100). Any suggestions how I can make it work?

Upvotes: 1

Views: 55

Answers (1)

Plamen Mathias
Plamen Mathias

Reputation: 36

You don't need to convert everything, use this:

location /list/ {
   rewrite ^/list/(.*)$ /index.php?m=$1 last;
}

Don't forget to restart nginx afterwards to see the changes.

Upvotes: 2

Related Questions