Paul S.
Paul S.

Reputation: 131

nginx rewrite if file exists with arguments

Have a rather basic query that I just can't figure out. Basically, what I'm trying to do is,

(Base URL used is: http://app.domain.tld/link?params=value

Firstly, if a file called link.php exists, I want the request to be forwarded to it while keeping all parameters that exist.

If no parameters exist, it should simply internally ('break') redirect to link.php.

If the file does not exist at all, it should be internally redirected to index.php?p=$uri, or something similar.

The preserving parameters part on existing files is what I'm having the most trouble with.

Does anyone have any pointers, or configuration examples that achieve something similar they would not mind sharing?

Much appreciated, and thanks to everyone who takes a moment to read this.

Upvotes: 1

Views: 1671

Answers (1)

Fleshgrinder
Fleshgrinder

Reputation: 16303

location ^~ /link {
  // do we have any arguments?
  if ($request_uri = $uri) {
    return 302 /link.php;
  }
  // do we have a link.php file?
  try_files /link.php @index;
}

location @index {
  return 302 /index.php?p=$uri;
}

location = /link.php {
  // do something
}

Upvotes: 1

Related Questions