28

I'm rewriting URLs in nginx after a relaunch. In the old site I had query parameters in the URL to filter stuff e.g.

http://www.example.com/mypage.php?type=4

The new page doesn't have these kind of parameters. I want to remove them and rewrite the URLs to the main page, so that I get:

http://www.example.com/mypage/

My rewrite rule in nginx is:

location ^~ /mypage.php {
    rewrite ^/mypage.php$ http://www.example.com/mypage permanent;
}

But with this rule the parameter is still appended. I thought the $ would stop nginx from processing further values... any ideas? All other questions deal with how to add parameters - I just want to remove mine :)

lorem monkey
  • 3,942
  • 3
  • 35
  • 49

5 Answers5

52

Had a similar problem, after a lot of searching the answer presented itself in the rewrite docs.

If you specify a ? at the end of a rewrite then Nginx will drop the original $args (arguments)

So for your example, this would do the trick:

location ^~ /mypage.php {
    rewrite ^/mypage.php$ http://www.example.com/mypage? permanent;
}
BHowell
  • 635
  • 6
  • 4
14

To drop a parameter from a URL, in this case coupon=xxx:

if ($query_string ~ "^(.*)coupon=(.*)$") {
    rewrite ^(.*)$ $uri? permanent;
}

Note that this will drop all parameters if the statement matches. $uri is the original request without parameters.

Luke Peterson
  • 8,584
  • 8
  • 45
  • 46
5

Try setting the $args variable to empty inside the location.

set $args '';
Undo
  • 25,519
  • 37
  • 106
  • 129
Daniel Pérez Rada
  • 1,441
  • 1
  • 13
  • 8
  • That will break this type of operation where you want to use a query parameter value to add a new header: proxy_set_header Authorization "Bearer $arg_token"; – Ron McLeod Nov 03 '20 at 17:40
5

If you want to remove a specified parameter from url,

#  in location directive: 
if ($request_uri ~ "([^\?]*)\?(.*)unwanted=([^&]*)&?(.*)") {
    set $original_path $1; 
    set $args1 $2; 
    set $unwanted $3; 
    set $args2 $4; 
    set $args ""; 

    rewrite ^ "${original_path}?${args1}${args2}" permanent;
}

then visit your_site.com/a=1&unwanted=2&c=3

step1. server gives an 302 response, indicating the url is match.

step2. client re-send a request with the new url ( with the parameter removed)

Siwei
  • 19,858
  • 7
  • 75
  • 95
  • Aren’t IF conditions considered evil in Nginx world? – Khom Nazid Aug 05 '19 at 08:45
  • Note. Use `break;` instead of `permanent;` to keep the same url in the browser. – Nux May 06 '20 at 21:57
  • @KhomNazid, they are evil only if you use them for things _other then rewrite_. See: https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/#what-to-do-instead – Nux May 06 '20 at 22:25
0

If we append a ? to the end of the destination it will prevent the query from being appended to the destination. :)

Example Source Args Destination
Keep the arg appended rewrite ^/mypage.php$ arg=something https://example.com/new-page/
Do not preserve the arg rewrite ^/mypage.php$ arg=something https://example.com/new-page/?
Exactly Rahul
  • 114
  • 10