9

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;
}

1 Answer 1

28

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;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

It works fine! Can you also explain me what you did? For each GET parameter I have a $arg_name variable?
@fede91it Read the official documentation.
great stuff thanks and very old answer. Wonder how can you just check that argument is only set :D, and ignore the value. I assume just remove = desktop part

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.