Archive

HowTo: Permanently redirect a request with parameter consideration in Tengine/NginX

Well, this one gave me a super hard time. I looked everywhere and found nothing. There is a lot of misinformation.

As usual, the Nginx and Funtoo communities helped me. Thanks to:

  • MTecknology in #nginx @ Freenode
  • Tracerneo in #funtoo @ Freenode

So, how do we do this? Easy, we use a map:

# get ready for long redirects
map_hash_bucket_size 256;
map_hash_max_size 4092;

# create the map
map $request_uri $newuri {
    default 0;
    /index.php?test=1 /yes;
    /index.php?test=2 https://google.com/;
}

server {
    listen *;
    server_name test.php.g02.org;
    root /srv/www/php/test/public;

    # permanent redirect
    if ($newuri) {
        return 301 $newuri;
    }

    index index.php index.html;
    autoindex on;

    include include.d/php.conf;

    access_log /var/log/tengine/php-access.log;
    error_log /var/log/tengine/php-error.log;
 }

So, basically, you want to use the $request_uri in order to catch the the uri with it's parameters. I wasted all day figuring out why $uri didn't have this. It turns out it discards the parameters... anyway.

This one was a hard one to find. Please, share and improve!