Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making nginx check parameter in url

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

like image 850
user2650277 Avatar asked Jun 02 '14 06:06

user2650277


People also ask

How do you specify a parameter in a URL?

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 (&).

Can I pass URL as query parameter?

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.

Can Nginx location blocks match a URL query string?

nginx location block doesn't match query string at all. So it's impossible. This directive allows different configurations depending on the URI.

What is Request_uri in nginx?

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.


1 Answers

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.

like image 138
Alexey Ten Avatar answered Oct 09 '22 19:10

Alexey Ten