I want to check if a parameter is present in a url in nginx and then rewrite.How can i do that?
For e.g if url is http://website.com/?mobile
then redirect user to http://m.website.com
To identify a URL parameter, refer to the portion of the URL that comes after a question mark (?). URL parameters are made of a key and a value, separated by an equal sign (=). Multiple parameters are each then separated by an ampersand (&).
Yes, that's what you should be doing. encodeURIComponent is the correct way to encode a text value for putting in part of a query string. but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.
nginx location block doesn't match query string at all. So it's impossible. This directive allows different configurations depending on the URI.
According to NGINX documentation, $request_uri is the original request (for example, /foo/bar. php? arg=baz includes arguments and can't be modified) but $uri refers to the altered URI.
You better use http://example.com/?mobile=1
(argument with value). In this case checking is simple:
if ($arg_mobile) { return 302 http://m.example.com/; }
Checking for argument existance is usually done with regexp like if ($args ~ mobile)
but it's error-prone, because it will match mobile
anywhere, e.g. http://example.com/?tag=automobile
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With