0

I have this line of code in my /etc/nginx/sites-available/default page:

rewrite ^/(.*)/(.*) /search.php?name=$1&last=$2

It accomplished what I wanted: cleaner urls for linking

example.com/john/smith is forwarded properly to the PHP GET.

But, it also destroyed all my styling and image links.

So I would like the rewrite to occur only if the first word is neither css nor image.

But I can only find if statements for GET arguments in the url not for directoies in the url like this:

if ($args ~* "/?param1=val1&param2=val2&") {
      rewrite ^ http://www.example.com/newparam/$arg_param1/$arg_param2? last;
}
Romulus
  • 1,534
  • 2
  • 11
  • 18
  • I think a better solution may be to only apply the rewrite if the requested filename doesn't exist. [Take a look at this question](http://stackoverflow.com/questions/595005/redirect-requests-only-if-the-file-is-not-found). – Austin Brunkhorst Jun 28 '16 at 18:33
  • 1
    @AustinBrunkhorst I would be willing to use that solution, but it seems to be for apache and my issue is getting the syntax right. Ill look for nginx solutions like that – Romulus Jun 28 '16 at 18:38

1 Answers1

1

After following the apache solution suggested in this comment:

"I think a better solution may be to only apply the rewrite if the requested filename doesn't exist. Take a look at this question. Redirect requests only if the file is not found? "

I converted that functionality to nginx and got:

location / {
    try_files $uri $uri/ =404;
    if (-f $request_filename) {
            break;
    }
    rewrite ^/(.*)/(.*) /search.php?name=$1&last=$2
}

This makes the url rewrite only if there is no file found at the given directory path

Community
  • 1
  • 1
Romulus
  • 1,534
  • 2
  • 11
  • 18