重定向移动设备的特定URL(NGINX)

时间:2017-02-07 09:26:44

标签: redirect nginx url-redirection

我正在尝试将主页(www.example.com)重定向到特定的移动着陆页(www.example.com/m)

我只希望重定向发生在移动设备上,而我正在使用NGINX

非常感谢任何帮助!感谢

2 个答案:

答案 0 :(得分:0)

可以使用完全匹配https://nerian.com/support/resources/calculator/语句隔离主页。使用user-agent标题上的正则表达式来标识要重定向到/m的条件。

例如:

location = / {
    if ($http_user_agent ~ some-regular-expression) {
        return 301 /m;
    }
    rewrite ^ /index.html;
}

有关详细信息,请参阅location;有关if的使用,请参阅this document

答案 1 :(得分:0)

您可以使用ngx_http_map_module创建其值取决于其他变量值的变量。

例如:

map $http_host $name {
   hostnames;

   example.com   1;
}

map $http_user_agent $mobile {

"~*iphone"    1;

}

从版本.0.9.6开始,您可以使用正则表达式〜*表示不区分大小写的正则表达式,〜表示区分大小写。这样您就可以使用用户代理映射您的主机名。

希望这可以帮助你。