1

i really need help here. Sitting for some time now and dont figured it out.

I want to realize a very simple task - rewrite a non-existent php file to another existant php file with all arguments like:

this  http://example.com/nonexistent.php?url=google.com
to -> http://example.com/existent.php?url=google.com

I tried something like this:

rewrite ^/nonexistent.php /existent.php;

Which dont works (File not found). But redirect a non-existent html file to a php file like this:

rewrite ^/nonexistent.html /existent.php;

works.

I dont want to rewrite a html file, but this is still a confusing behaviour.

Therefore it tried also something like this (and some variations):

rewrite ^/nonexistent.php?url=^(.*)$ /existent.php?url=$1;

which is also not working. (Maybe the syntax is bad)

Any help here? It would be very nice!

carrot
  • 77
  • 1
  • 1
  • 10
  • Nginx stores the GET parameters in a variable named [`$args`](http://wiki.nginx.org/HttpCoreModule#.24args). You should be able to append it to your redirect: `/existent.php?$args` – cyberx86 Nov 27 '12 at 12:17
  • Quite confused with what you're asking. Can you try to rephrase the question? I don't know what you mean by non-existent php file. Are you saying that if the requested file by the user does not exist, it should try another file? – Grumpy Nov 27 '12 at 12:24
  • Ok, the request url is the following: `http://example.com/nonexistent.php?url=google.com` . In this case, nonexistent.php doesnt exists and should be rewritten with all its arguments to a existing file like `http://example.com/existent.php` which will be resulting in `http://example.com/existent.php?url=google.com` . I tried `rewrite ^/api.php /API/simple.php?$args;` which doesnt works. In .htaccess it is `RewriteRule ^nonexistent.php$ existent.php [QSA,L]` . – carrot Nov 27 '12 at 22:35
  • Could you provide your full config? The problem with the `rewrite` directive is likely result of your configuration. – VBart Nov 28 '12 at 03:33

1 Answers1

2
location = /nonexistent.php {
    rewrite ^ /existent.php last;
}
VBart
  • 8,159
  • 3
  • 24
  • 25