开发者

Nginx - Need regex to append a parameter to url

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/?a开发者_如何学运维bc=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.


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*)


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜