nginx 301重定向查询字符串

时间:2016-11-25 17:46:09

标签: redirect nginx

目前我的nginx.conf文件中有类似的内容:

location ~ /old/page/?$ {
    return 301 /new-page;
}

问题是查询字符串是从/ old / page?ref = xx URL中删除的。

是否可以使用我上面使用的重定向方法包含查询字符串?

1 个答案:

答案 0 :(得分:15)

?及之后的任何内容都是查询字符串,不属于locationrewrite指令中使用的规范化URI的一部分。有关详细信息,请参阅this document

如果要保留查询字符串,请将其添加到return

location = /old/page/ {
    return 301 /new/page$is_args$args;
}

或者使用rewrite,除非添加?,否则会自动追加查询字符串:

rewrite ^/old/page/$ /new/page permanent;

有关位置语法,请参阅this document,有关返回/重写的信息,请参阅this document