NGINX 301 使用查询参数值重定向 [英] NGINX 301 redirect using query argument value

查看:71
本文介绍了NGINX 301 使用查询参数值重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 example.com/?lang=en 重定向到 example.com/en/.

I want to redirect example.com/?lang=en to example.com/en/.

我使用 Python Django 和我的服务器运行 Plesk/Nginx.

I am using Python Django and my server running Plesk / Nginx.

我尝试像这样重定向我的网页.但它不起作用;

I try to redirect on my webpage like this. But it's don't work;

rewrite ^/?lang=en$ /en/ redirect;

但是如果我删除问号重写是有效的.

But if i remove question mark rewrite is worked.

我尝试了很多方法,但找不到解决方案.

I tried many methods but I couldn't find a solution.

谢谢.

推荐答案

最简单的是

if ($arg_lang = en) {
    return 301 /en$uri;
}

但是,如果您有任何其他查询参数,它们将因此重定向规则而丢失.要保留所有其他查询参数,您可以执行以下操作:

However if you'd have any other query arguments, they would lost with this redirection rule. To preserve all the other query arguments you can do the following:

if ($args ~ (.*)(^|&)lang=en(\2|$)&?(.*)) {
    set $args $1$3$4;
    return 301 /en$uri$is_args$args;
}

为了支持多种语言,我想到的第一个解决方案是

To support several languages the first solution came to mind is

if ($args ~ (.*)(^|&)lang=([^&]*)(\2|$)&?(.*)) {
    set $args $1$4$5;
    return 301 /$3$uri$is_args$args;
}

但是,如果您有一些格式错误的 lang 查询参数值,它会导致重定向到不存在的页面.要仅过滤支持语言的 lang 值,您可以使用 map 指令:

However if you'd have some malformed lang query argument value it would lead to redirection to non-existent page. To filter lang values for supported languages only you can use the map directive:

map $arg_lang $prefix {
    en    /en;
    de    /de;
    ...
    # if none matched, value of $prefix variable would be an empty string
}
map $args $stripped_args {
    # remove "lang" query argument if exists
    ~(.*)(^|&)lang=[^&]*(\2|$)&?(.*)  $1$3$4;
    default                           $args;
}
server {
    ...
    if ($prefix) {
        set $args $stripped_args;
        return 301 $prefix$uri$is_args$args;
    }
    ...
}

如果您的 URI 语言前缀与 lang 查询参数值相同(或者可以通过一些正则表达式从中派生),则第一个 map 块可能是简化:

If your URI language prefix is the same as the lang query argument value (or can be derived from it through some regular expression), the first map block could be simplified:

map $arg_lang $prefix {
    ~^(en|de|...)$    /$1;
}

更新

正如 OP 所述,当我们收到像 example.com/de/some/path/?lang=en 这样的请求会被重定向到不存在的页面时,可能会有一个警告example.com/en/de/some/path/.为了避免这种情况,我们可以定义额外的 map 块并从 URI 中去除语言前缀:

As OP states, there could be a caveat when we've got a request like example.com/de/some/path/?lang=en which would be redirected to non-existent page example.com/en/de/some/path/. To avoid it we could define additional map block and strip the language prefix from the URI:

map $arg_lang $prefix {
    ~^(en|de|...)$    /$1;
}
map $args $stripped_args {
    # remove "lang" query argument if exists
    ~(.*)(^|&)lang=[^&]*(\2|$)&?(.*)  $1$3$4;
    default                           $args;
}
map $uri $stripped_uri {
    # remove language prefix from URI if one exists
    ~^/(en|de|...)(/.*)$  $2;
    default               $uri;
}
server {
    ...
    if ($prefix) {
        set $args $stripped_args;
        return 301 $prefix$stripped_uri$is_args$args;
    }
    ...
}

这篇关于NGINX 301 使用查询参数值重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆