【问题标题】:Nginx location redirect but saving the arguments passedNginx 位置重定向但保存传递的参数
【发布时间】:2021-12-01 04:05:05
【问题描述】:

我试图在保留 URL 查询参数的同时将用户重定向到新 URL。我正在尝试这个,它没有传递 URL 参数:

location = /api/redirects {
  return 301 /api2/redirects;
}
https://example.com/api/redirects?param=1&anotherParam=10

  => https://example.com/api2/redirects

我也试过了:

location = /api/redirects {
  return 301 /api2/redirects$is_args$args;
}

【问题讨论】:

  • 您是否在进行配置更改后重新启动了 Nginx 并重置了浏览器缓存。您的$is_args$args 修复应该有效。与其依赖浏览器,不如使用curl -I http://example.com/api/redirects 进行测试
  • @LearningPath 与return 指令相反,rewrite 将保留任何现有的查询参数:rewrite ^ /api2/redirects permanent;

标签: nginx redirect url-rewriting


【解决方案1】:

您可以在 location 块之外显式使用 rewrite。 RegEx 括号可以将/api/ 之后的URI 值捕获为$1。查询字符串始终在 rewrite 指令中传递。

# 302 Moved Temporarily
rewrite ^/api/(redirects.*) /api2/$1 redirect;
# 301 Moved Permanently
rewrite ^/api/(redirects.*) /api2/$1 permanent;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-06
    • 2021-07-22
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多