I have been stuck on a regex. I want to append foo=1 to all URLs.
For a URL like:
www.xyz.com/
Rewrite:
www.xyz.com/?foo=1
For a URL like:
www.xyz.com/?abc=2
Rewrite:
www.xyz.com/?abc=2&foo=1
try putting your variable before the args,
location /whatever {
try_files $uri /index.php?foo=1&$args;
}
This way if args are empty you'll have foo, if not it will be appended.
Putting $args first might make your request end up looking like this
www.xyz.com/?&foo=1
because $args were first and they were empty, and I'm not sure if that will be handled correctly or not.
Mind also that if you explicitly define foo the value you entered will be overwritten by nginx.
www.xyz.com/?foo=new_value&foo=1
foo=1 will overwrite the new value, if you want to protect that value putting $args at the end will help you do that.
In Nginx this is not achieved by rewrite, as it operates only on the path portion of the URI. Instead you have to modify the $args variable:
set $args $args&foo=1
$args is empty, this is a problem