【问题标题】:Nginx - Rewrite using last url segmentNginx - 使用最后一个 url 段重写
【发布时间】:2016-07-22 00:47:48
【问题描述】:

我的原始网址如下:

http://example.com/lab/file.php?id=4&idl=42

我想美化它,使其也可以通过如下网址访问:

http://example.com/l/not/important/url/part/?id=4&idl=42

我尝试了以下代码,但它不起作用。

rewrite /l/.*/([^/]+)/?$ /lab/file.php$1 last;

如何解决?

【问题讨论】:

    标签: regex nginx url-rewriting


    【解决方案1】:

    ? 之后的任何内容都是查询字符串,不能使用 rewrite 指令重写。但是,默认情况下,rewrite 无论如何都会附加原始查询字符串。

    您可以重写任何以/l/ 开头的 URI 或为/l/ 创建一个位置,例如:

    rewrite ^/l/ /lab/file.php last;
    

    或:

    location ^~ /l/ {
        rewrite ^ /lab/file.php last;
    }
    

    ^~ 修饰符使此前缀位置优先于同一级别的正则表达式位置。详情请见this document

    在这两种情况下,rewrite 都会将原始查询字符串附加到重写的 URI。详情请见this document

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      相关资源
      最近更新 更多