Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 access www.domain.com/folder/folder1/blahblah to be redirected to the above URL without the url changing.

Thus i have the following .htaccess in the folder/folder1/ which works perfectly:

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/ results in:

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 neither works. I tried inserting them either in

location / {
}

or in

location /folder/folder1 {
}

or in

location ~ \.php$ {
}

in all locations i get a 404 error

nginx error reports either "primary script unknown while reading response header from upstream" or "no such file or directory".

can someone enlighten me please ?

thanks in advance!

like image 668
MirrorMirror Avatar asked Jan 29 '13 09:01

MirrorMirror


Video Answer


1 Answers

First, don't use if. If is evil -> http://wiki.nginx.org/IfIsEvil

You can accomplish this by using the following rewrite rule.

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

Place this rewrite rule just above you location / {} block

like image 84
Jap Mul Avatar answered Sep 28 '22 16:09

Jap Mul