【问题标题】:Rewrite trailing ampersand in nginx, without a 301重写 nginx 中的尾随 & 符号,没有 301
【发布时间】:2020-02-11 01:47:41
【问题描述】:

我正在寻找 nginx 从 URL 中删除尾随“&”而不返回 301 的解决方案。

例子:

有可能吗?

我尝试过没有成功

if ($request_uri ~ ^(.*)\?&$) {
  set $pagepath $1;
  rewrite ^ /$1 break;
}

【问题讨论】:

  • & 是查询字符串 - 没有查询字符串的 URI 已经可以用作 $uri - 您不需要重写它。
  • 正如@RichardSmith 已经提到的,重写URI 不会影响您的查询字符串,您应该改为更改$args 变量。您是否需要保留其他查询变量,即将https://example.com&a=b& 请求处理为https://example.com?a=b
  • @IvanShatsky 是的,我想保留任何其他查询参数。短篇小说,以“&”结尾的 URL 搞砸了一些事情,如果可能的话,我想在 nginx 级别重写它们,但不返回 301。

标签: nginx url-rewriting


【解决方案1】:

试试这个:

map $args $stripped_args {
    ~(.*)&$  $1;
    default  $args;
}

server {
    ...
    set $args $stripped_args;
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2013-07-12
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    相关资源
    最近更新 更多