nginx实现多语言跳转不同的url

Posted cameron

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx实现多语言跳转不同的url相关的知识,希望对你有一定的参考价值。

nginx实现多语言跳转不同的url

server {
        listen       80;
        server_name www.text.com;

        location / {

            if ($http_accept_language ~* ^zh)       {
                set $lang zh_CN;
                rewrite (.*)  https://www.baidu.com$1 permanent;
                break;
            }

            if ($http_accept_language ~* ^ko)       {
                set $lang ko_KR;
                rewrite (.*)  https://www.aliyun.com$1 permanent;
                break;
            }
            if ($http_accept_language ~* ^en)       {
                set $lang en_US;
                rewrite (.*)  https://www.google.com$1 permanent;
                break;
            }
        }

}

测试

curl -I http://www.text.com -H "Accept-Language:ko"
curl -I http://www.text.com -H "Accept-Language:zh"
curl -I http://www.text.com -H "Accept-Language:en"


$ curl -I http://www.text.com -H "Accept-Language:zh"
HTTP/1.1 301 Moved Permanently
Server: WPWS/1.0.0
Date: Wed, 06 Mar 2019 06:13:29 GMT
Content-Type: text/html
Content-Length: 281
Connection: keep-alive
Location: https://www.baidu.com/
Expires: Sat, 16 Mar 2019 06:13:29 GMT
Cache-Control: max-age=864000


$ curl -I http://www.text.com -H "Accept-Language:ko"
HTTP/1.1 301 Moved Permanently
Server: WPWS/1.0.0
Date: Wed, 06 Mar 2019 06:14:05 GMT
Content-Type: text/html
Content-Length: 281
Connection: keep-alive
Location: https://www.aliyun.com/
Expires: Sat, 16 Mar 2019 06:14:05 GMT
Cache-Control: max-age=864000

$ curl -I http://www.text.com -H "Accept-Language:en"
HTTP/1.1 301 Moved Permanently
Server: WPWS/1.0.0
Date: Wed, 06 Mar 2019 06:20:56 GMT
Content-Type: text/html
Content-Length: 281
Connection: keep-alive
Location: https://www.google.com/
Expires: Sat, 16 Mar 2019 06:20:56 GMT
Cache-Control: max-age=864000

以上是关于nginx实现多语言跳转不同的url的主要内容,如果未能解决你的问题,请参考以下文章

Nginx rewrite 重写功能和Nginx的正则表达式

nginx url地址重写功能

PHP代码里有没有办法实现让页面跳转,但地址栏上的URL不变

端口跳转工具都有哪些

nginx 301跳转,怎么把refere带过去

Nginx 实现 Rewrite 跳转