1

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

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

The one below is for your first url

([w]{3}\.[a-zA-Z0-9\-]+\.([a-zA-Z0-9]{3,4}|[a-zA-Z]{2}|[a-zA-Z0-9]{3,4}\.[a-zA-Z]{2}))/?

This is for the second url

([w]{3}\.[a-zA-Z0-9\-]+\.([a-zA-Z0-9]{3,4}|[a-zA-Z]{2}|[a-zA-Z0-9]{3,4}\.[a-zA-Z]{2}))/?(\?\S*=\S*)

Comments

-1

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

1 Comment

if $args is empty, this is a problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.