Nginx rewriting URL with params to url without params
So I have this URL: /web/cms.php?open=unsereteam
I want to redirect to: https://www.camper-center.ch/portrait/unser-team.html
I tried this:
rewrite ^/web/cms.php?open=unsereteam$ https://www.camper-center.ch/portrait/unser-team.html permanent;
and this:
if ($args ~* "/web/cms.php?open=unsereteam") {
rewrite ^ https://www.camper-center.ch/portrait/unser-team.html? last;
}
doesnt work...
Thanks to Ivan Shatsky it worked with:
if ($request_uri = "/web/cms.php?open=unsereteam") {
rewrite ^ https://www.camper-center.ch/portrait/unser-team.html? last;
}
If I have more than one param like
if ($request_uri = "/web/listing.php?monat=03&jahr=2018") {
rewrite ^ https://www.camper-center.ch/news/aktuell.html?month=03&year=2018? last;
}
Im triying something like:
if ($arg_monat = "02" & $arg_jahr="2020") {
rewrite ^/web/cms\.php$ https://www.camper-center.ch/news/aktuell.html?month=02&year=2020? last;
}
But this obviously doesn't work too.
Maybe you have a Solution for this too?
Thanks in advance!
Edit: Im trying something like this:
if ($arg_monat && $arg_jahr ) {
rewrite ^/web/listing\.php$ https://www.camper-center.ch/news/aktuell.html?month=$arg_monat&year=$arg_jahr? last;
}
in the end I want the first url to redirect to the second:
/web/listing.php?monat=02&jahr=2020
to
/news/aktuell.html?month=02&year=2020
As being told a thousand times, both rewrite
and location
directives works with the normailzed URI which does not include the query part of the request, but only the /web/cms.php
for your case. The $args
variable contains the query part of the request (open=unsereteam
for your case). It is a $request_uri
that contains the full request URI in its original state. You can use
if ($request_uri = "/web/cms.php?open=unsereteam") {
rewrite ^ https://www.camper-center.ch/portrait/unser-team.html? last;
}
or for case insensitive comparsion
if ($request_uri ~* "^/web/cms\.php\?open=unsereteam") {
rewrite ^ https://www.camper-center.ch/portrait/unser-team.html? last;
}
but it won't match if your request would have, lets say, some additional query argument before the open
one. The better one is
if ($arg_open = "unsereteam") { # or ($arg_open ~* "^unsereteam$") for case insensitive check
# do the rewrite only on '/web/cms.php' URI
rewrite ^/web/cms\.php$ https://www.camper-center.ch/portrait/unser-team.html? last;
}