5

In my default site config file, I have the following redirect to enforce https :

 server {
    listen       80;
    server_name  www.example.com example.com;
    return       301 https://example.com$request_uri;
 }

I want to add a subdomain, but to redirect it to the site wit a parameter. e.g. fr.example.com --> https://example.com?lang=fr

If I do:

return       301 https://example.com$request_uri&lang=fr;

It will add on '&lang=fr' whether there are any other parameters in $request_uri or not.

How do I conditionally define '?' or '&', based on the content of $request_uri ?

I tried the following:

server {
    listen       80;
    server_name  fr.example.com;
       if ($request_uri ~ ""){
           return       301 https://example.com?tlang=fr;
       }
        return       301 https://example.com$request_uri&tlang=fr;
}

but like this the site failed all-together.

Thanks

Avi Kehat
  • 51
  • 1
  • 1
  • 2

1 Answers1

1

First of all, $request_uri does not contain query arguments for the request.

There are two options:

  1. Use a return where you add $args after the lang argument:

return 301 https://example.com?lang=fr&$args:

  1. Use a map:

In http level, you define the map:

map $args $redirargs {
    "~.+" $args&lang=fr;
    default lang=fr;
}

And then use

return 301 http://example.com$request_uri?$redirargs;
Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63