1

I searched on so many threads without any luck, maybe you can help me.

I try to redirect this link https://www.example.com/page/5/?s=gluten to https://www.example.com/gluten_specificarticle

I tried to use map solution but it does not work

map $arg_s $mypage {
    gluten /specificlink;
}

There is 2 variables in the link : '5' for page and '?s=gluten' for the search argument.

Thank you for any help

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58

1 Answers1

1

The $request_uri variable contains the entire URI including the query string. You will need to use an if block to test the condition.

For just one redirection, you could use:

if ($request_uri = "/page/5/?s=gluten") { 
    return 301 /gluten_specificarticle; 
}

If you have a number of redirections, use a map:

map $request_uri $redirect {
    /page/5/?s=gluten    /gluten_specificarticle; 
}
server {
    ...
    if ($redirect) {
        return 301 $redirect;
    }
    ...
}

See this document for details. See this caution on the use of if.

Richard Smith
  • 11,859
  • 2
  • 18
  • 26
  • This is great thank you very much Richard ;-) – user1437654 Aug 10 '20 at 09:26
  • However using the map function, i have an error and can't reload nginx. map $request_uri $redirect { /page/5/?s=gluten /xxx/maladies/intolerance-et-sensibilite-au-gluten/; /page/3/?s=gluten /xxx/maladies/intolerance-et-sensibilite-au-gluten/; /page/2/?s=gluten /xxx/maladies/intolerance-et-sensibilite-au-gluten/; /page/4/?s=gluten /xxx/maladies/intolerance-et-sensibilite-au-gluten/; } server { ... if ($redirect) { return 301 $redirect; } ... } – user1437654 Aug 10 '20 at 09:41
  • I have one last question ;-) What if the url contain a space in it. I read that you must use a quote so i tried like this if ($request_uri = "'/page/5/?s=Vitamine D'") { return 301 /xxx/vitamines/vitamine-d/; } Space between vitamine and D But it doesn't work Thanks – user1437654 Aug 10 '20 at 09:52
  • In my example, it was already within double quotes. However, the URI in `$request_uri` will probably be encoded, so spaces may be replaced by `+` or `%20`. – Richard Smith Aug 10 '20 at 09:58
  • For the `map` problem - the `map` statement goes outside your `server` block. Use `nginx -t` to test the configuration and view the syntax error. – Richard Smith Aug 10 '20 at 10:00
  • I don't have enough privilege to open the error.log unfortunately It mention nothing for my case in the console – user1437654 Aug 10 '20 at 10:04