nginx重写保留CGI查询参数(带有哈希锚)

对于我的www.example.com nginx配置,我有以下重写规则:

rewrite ^/foo$       https://one.example.com/page#one     permanent;
rewrite ^/foo(\?.*)$ https://two.example.com/page$1#two   permanent;

rewrite ^/bar$       https://three.example.com/page#one   permanent;
rewrite ^/bar\?(.*)$ https://four.example.com/page?$1#two permanent;

http://www.example.com/foo的请求正确重定向到https://one.example.com/page#one

http://www.example.com/bar的请求正确重定向到https://three.example.com/page#one

http://www.example.com/foo?extra=yes的请求不正确 重定向到https://one.example.com/page#one?extra=yes(我希望它转到https://two.example.com/page?extra=yes#two)。

http://www.example.com/bar?extra=yes的请求不正确 重定向到https://three.example.com/page#one?extra=yes(我希望它转到https://four.example.com/page?extra=yes#two)。

如何重定向到复制CGI参数并链接到目标页面中特定锚点的页面?

yhj861212 回答:nginx重写保留CGI查询参数(带有哈希锚)

在将查询字符串组装为替换字符串时,rewrite指令似乎无法正确处理#片段。

您可以通过在替换字符串后添加rewrite来防止?附加查询字符串。因此,您可以使用内置变量$is_args$args构造正确的结果。

例如:

rewrite ^/foo$ https://one.example.com/page$is_args$args#one? permanent;

有关详细信息,请参见this document

请注意,查询字符串不是用于匹配rewritelocation语句的规范化URI的一部分,因此您的^/foo(\?.*)$正则表达式将不起作用。

本文链接:https://www.f2er.com/3164086.html

大家都在问