【问题标题】:Nginx - 301 redirect for pages with specific GET param to the same page without get paramsNginx - 301 将具有特定 GET 参数的页面重定向到没有获取参数的同一页面
【发布时间】:2019-06-05 07:37:05
【问题描述】:

我有

  • 许多页面被搜索引擎索引,带有蹩脚的 GET 参数,例如 _escaped_fragment_(有关转义片段的更多信息,请参阅 Yandex man page
  • nginx 作为许多不同前端应用程序前面的反向代理

所以我只需要为所有这些页面获取 301 重定向,这些页面带有一些 GET 参数到相同的页面,但没有任何 get 参数。例如

example.com/some/long/path?_escaped_fragment_=

应该被301重定向到

example.com/some/long/path

我可以通过将这个逻辑添加到每个前端应用程序来做到这一点,或者我可以在 nginx 配置中做到这一点。我更喜欢使用第二种变体。

可能的解决方案可以包括

【问题讨论】:

    标签: nginx redirect url-rewriting


    【解决方案1】:

    我已经以这种方式解决了我最初的问题

    • 将以下代码添加到我的http context 以定义来自$request_uri 的不带参数的请求uri 路径(这里我们使用map module

      map $request_uri $request_uri_path {
          "~^(?P<path>[^?]*)(\?.*)?$"  $path;
      }
      
    • 将下面的代码添加到我的server context 以使301 redirect 从带有_escaped_fragment_ 在GET 参数的页面到没有任何GET 参数的相同页面(这里我们使用rewrite module

      if ($args ~*  "_escaped_fragment_") {
          rewrite ^ $scheme://$host$request_uri_path? permanent;
      }
      

    这对我有用。

    【讨论】:

      猜你喜欢
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 2021-12-28
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多