0

So at the moment this is working for me:

if ($request_uri = "/web/news.php?id=69") {
    rewrite ^ https://www.camper-center.ch/? last;
}

But now I also have URLs like /web/listing.php?monat=02&jahr=2020 with two Parameters instead of one like above.

if ($request_uri = "/web/listing.php?monat=02&jahr=2020") {
    rewrite ^ https://www.camper-center.ch/news/aktuell.html?month=02&year=2020? last;
}

this doesn't seem to work. Do you have any suggestions?

Since it redirected me to the site with german parameters, I redirected them and it worked like this for me in the end:

if ($request_uri = "/news/aktuell.html?monat=02&jahr=2020") {
    rewrite ^ https://www.camper-center.ch/news/aktuell.html?month=02&year=2020? last;
}

2 Answers 2

0

Try:

if ($args ~* "/web/listing.php?monat=02&jahr=2020") {
    rewrite ^ https://www.camper-center.ch/news/aktuell.html?month=$arg_monat&year=$arg_jahr? last;
}

https://nginx.org/en/docs/http/ngx_http_core_module.html#variables

Just adapt to your needs.

2
  • it somehow redirects me to camper-center.ch/news/aktuell.html?monat=02&jahr=2020 instead of month and year.. Commented Dec 3, 2020 at 7:37
  • @ac_s_fer The $args variable value can be either monat=02&jahr=2020 or jahr=2020&monat=02 but not the /web/listing.php?monat=02&jahr=2020, your solution is close to the working one but would not work because of reasons mentioned above. Commented Dec 3, 2020 at 8:16
0

You could try the following approach. Add a map into http level:

map $arg_id $idmap {
    default 0;
    "69" 1;
}

map $arg_monat $monatmap {
    default 0;
    "02" 1;
}

map $arg_jahr $jahrmap {
    default 0;
    "2020" 1;

Then use following if blocks:

if ($idmap = 1) {
    rewrite ^ https://www.camper-center.ch/? last;
}

if ($jahrmap$monatmap = "11") {
    rewrite ^ https://www.camper-center.ch/news/aktuell.html?month=02&year=2020 last;
}

map maps contents of input variable to output variable. $arg_id takes query argument id from URI. In the map above, nginx compares the id argument to 69. If it matches, $idmap gets value 1. Otherwise it gets value 0.

Arguments monat and jahr are handled similarly. Their output variables are concatenaed in if comparison, and if both arguments match values specified in map, then the rewrite is performed.

2
  • I made an http{} containing the maps. That kills all links on my Site. I dont have/see an already available http-level. How can I access it without breaking everything? Commented Dec 9, 2020 at 13:27
  • The http level usually exists in nginx.conf file. Depending on your distribution, you can add the map blocks to a file that is included in http level. Commented Dec 9, 2020 at 17:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .