5

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!

1
  • rewrite ^(.*(?![\.js|\.css])[^.]+)$ /index.php/$1 last; rewrites all ignoring only js and css files ... I was brought here with that problem. Commented May 27, 2013 at 2:55

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

this kind of works. it has two problems: a) it does not avoid the problem of calling /folder/folder1/existing_file.txt b) the css is screwed up, no css at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.