Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx redirect URL with specific query parameter

Nginx, I am trying to permanently redirect the URLs with a device GET parameter (http://www.example.org/page?device=desktop) to the relative URL without this parameter (http://www.example.org/page).

I did this, but it doesn't work.

location {
    rewrite ^(.*)\?device=desktop $1 permanent;
}
like image 812
Federico Liva Avatar asked Nov 22 '14 15:11

Federico Liva


1 Answers

Each query parameter is exposed as a variable prefixed with $arg_ in the configuration file. For example, device would become $arg_device. Using this you can make the comparison check within your location block, for example:

location / {
    if ($arg_device = desktop) {
        return 301 $uri;
    }
}
like image 143
Xavier Lucas Avatar answered Oct 14 '22 00:10

Xavier Lucas