Nginx php file rewrite url

so with apache I have a folder:

www.domain.com/folder/folder1/index.php?word=blahblah

and I want users who accessed www.domain.com/folder/folder1/blahblah to be redirected to the above URL without changing the URL.

So I have the following .htaccess in folder / folder1 /, which works fine:

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ index.php?word=$1 

So, I want to achieve the same functionality with nginx, and I used two converters: http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ leads to:

 if (!-f $request_filename){ set $rule_0 1$rule_0; } if (!-d $request_filename){ set $rule_0 2$rule_0; } if ($rule_0 = "21"){ rewrite ^/(.+)$ /index.php?word=$1; } 

and http://winginx.com/htaccess results in:

  if (!-e $request_filename){ rewrite ^(.+)$ /index.php?word=$1; } 

Now, I tried with both, but it doesn’t work. I tried to insert them into either

 location / { } 

or

 location /folder/folder1 { } 

or

 location ~ \.php$ { } 

in all places I get 404 error

nginx reports the error "primary script unknown when reading the response header from the upstream" or "there is no such file or directory."

can someone enlighten me please?

early!

+4
source share
1 answer

First, do not use if . If evil β†’ http://wiki.nginx.org/IfIsEvil

You can accomplish this using the following rewrite rule.

 rewrite ^/folder/folder1/(.*)$ /folder/folder1/index.php?word=$1 last; 

Put this rewrite rule right above you location / {} block

+6
source

All Articles