Nginx rewrite to fixed destination link

Deckard asked:

I would like to rewrite all incoming links to a fixed destination link.
Any arguments and URI paths should be dropped, so

http://domain1.com/blah/blah.html?test=3&test2=4

should always become

http://newdomain.com/index.php

It seems so trivial but I don’t seem to get it – I always end up with at least the URI path being taken over.


I answered:

You should not be using rewrites for this at all; nginx is not apache.

Instead, use try_files.

This construction is common; it serves up static files directly, and then passes anything else to /index.php:

 location / {
    try_files $uri $uri/ /index.php;
 }

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.