在我的默认站点configuration文件中,我有以下redirect来执行https:
server { listen 80; server_name www.example.com example.com; return 301 https://example.com$request_uri; }
我想添加一个子域名,但redirect到一个参数的网站。 例如fr.example.com – > https://example.com?lang=fr
如果我做:
return 301 https://example.com$request_uri&lang=fr;
它会在'&lang = fr'上添加$ request_uri中是否有其他参数。
我如何有条件地定义'?' 或'&',基于$ request_uri的内容?
我尝试了以下内容:
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; }
但是像这样这个网站一起失败了。
谢谢
首先, $request_uri不包含请求的查询参数。
有两个选项:
lang参数之后添加$args地方使用返回值: return 301 https://example.com?lang=fr&$args:
在http级别中,您可以定义地图:
map $args $redirargs { "~.+" $args&lang=fr; default lang=fr; }
然后使用
return 301 http://example.com$request_uri?$redirargs;