【问题标题】:Regular expression match NGINX url segments from $request_uri variable正则表达式匹配来自 $request_uri 变量的 NGINX url 段
【发布时间】:2017-10-12 19:01:06
【问题描述】:

希望对这个重定向过程有所了解

我希望重定向所有 URL,除了匹配以下内容的 URL。

https://www.something.com/about-us/foo/(单页) 和 https://www.something.com/foo/some-arbitrary-segment/(很多页面都是这种结构)

这些是站点地图中唯一包含“foo”的网址,所以我试图简单地匹配它。

我首先需要检查另一个条件变量(我正在检查移动设备的用户代理)- 除了包含“foo”的页面之外的所有页面都应该重定向到新域。

set $mobile_redirect NO;

if (someconditionaluseragentstuff = someconditionaluseragentstuff) {
set $mobile_redirect YES;
}

if ($request_uri ~ "foo") {
set $mobile_redirect NO;
}

if ($mobile_redirect = YES) {
rewrite ^ https://www.somemobiledomain.com$request_uri? redirect;
break;
}

【问题讨论】:

    标签: regex redirect nginx


    【解决方案1】:

    location / 块通常是匹配以前不匹配另一个 location 块的 URI 的失败。并使用01 来代表更干净的配置文件的真/假。

    Rember 301 是永久重定向,302 是临时的

    set $mobile_redirect 0;
    
    if ($http_user_agent ~ "netscape navigator") {
      set $mobile_redirect 1;
    }
    
    # Specific URIs /about-us/foo/ and /foo/*
    location ~ ^/(about-us/foo/$|foo/)  {
      # Do nothing
    }
    
    # Every other URI
    location / {
      if ($mobile_redirect) {
        return 301 https://website.com$request_uri;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多