将查询参数附加到特定 URI

发布于 2025-01-10 15:05:33 字数 1420 浏览 0 评论 0原文

我正在尝试将查询字符串 ?sort_by=id&dir=ASC&lim=2&flt=1 附加到前往 /admin/users.php 的任何流量,这应该导致:

/admin/users.php?sort_by=id&dir=ASC&lim=2&flt=1

我试图遵循这个问题/答案:

https://serverfault.com/questions/311487/ nginx-rewrite-append-a-parameter-at-the-end-of-an-url

通过执行以下操作:

location /admin/users.php {
  rewrite  ^(.*) $1?sort_by=id&dir=ASC&lim=2&flt=1 break;
}

也尝试过:

  location /admin/users.php {
    rewrite  ^ /admin/users.php?sort_by=id&dir=ASC&lim=2&flt=1 last;
  }

但任何流量都会流向 /admin/users.php 回来了没有添加查询字符串。我显然做错了什么。

有什么正确实施的建议吗?


default.conf 完整内容:

server {
  listen 4000;
  root   /usr/share/nginx/html/src;

  include /etc/nginx/default.d/*.conf;

  index app.php index.php index.html index.htm;

  client_max_body_size 500m;


  location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    rewrite  ^/admin/?(.*) /$1 break;
    rewrite ^/admin/users.php$ /admin/users.php?sort_by=id&dir=ASC&lim=2&flt=1 break;
    fastcgi_param HTTP_PROXY "";
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
}

I'm trying to append the query string ?sort_by=id&dir=ASC&lim=2&flt=1 to any traffic going to /admin/users.php, which should result in:

/admin/users.php?sort_by=id&dir=ASC&lim=2&flt=1

I was attempting to follow this question/answer:

https://serverfault.com/questions/311487/nginx-rewrite-append-a-parameter-at-the-end-of-an-url

By doing the following:

location /admin/users.php {
  rewrite  ^(.*) $1?sort_by=id&dir=ASC&lim=2&flt=1 break;
}

Also tried:

  location /admin/users.php {
    rewrite  ^ /admin/users.php?sort_by=id&dir=ASC&lim=2&flt=1 last;
  }

But any traffic going to /admin/users.php comes back without the query string added. I'm obviously doing something wrong.

Any suggestions for implementing this correctly?


The default.conf in its entirety:

server {
  listen 4000;
  root   /usr/share/nginx/html/src;

  include /etc/nginx/default.d/*.conf;

  index app.php index.php index.html index.htm;

  client_max_body_size 500m;


  location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    rewrite  ^/admin/?(.*) /$1 break;
    rewrite ^/admin/users.php$ /admin/users.php?sort_by=id&dir=ASC&lim=2&flt=1 break;
    fastcgi_param HTTP_PROXY "";
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍景流年 2025-01-17 15:05:33

由于您正在尝试重写 PHP 文件的 URI,因此很可能您有一个 PHP 处理程序,看起来有点像

location ~ \.php$ {
    ...
}

~ 修饰符指定正则表达式匹配位置,并且该类型的位置比前缀(只有带有 = 修饰符的精确匹配位置比正则表达式具有更高的优先级)。此外,您不应该尝试使用附加位置块来完成此操作 - 使用该方法所能实现的只是无限循环。请尝试以下操作:

location ~ \.php$ {
    rewrite ^/admin/users.php$ /admin/users.php?sort_by=id&dir=ASC&lim=2&flt=1 break;
    include fastcgi_params;
    ...
}

更新

您的 PHP 处理程序位置中有两个 rewrite 指令。第一个将从任何以该前缀开头的 URI 中截断 /admin 前缀。第二个将向 /admin/users.php URI 添加额外的查询参数,但它已经被重写为 /users.php。此外,两个rewrite指令都有一个break标志,这意味着如果执行第一个规则,则永远不会到达第二个规则。要组合这两个规则,请尝试以下保留 rewrite 指令顺序:

rewrite ^/admin/users.php$ /users.php?sort_by=id&dir=ASC&lim=2&flt=1 break;
rewrite ^/admin/?(.*) /$1 break;

Since you are trying to rewrite an URI of the PHP file, most likely you have a PHP handler looking somewhat like

location ~ \.php$ {
    ...
}

The ~ modifier specifies a regex matching location, and locations of that type have a greater priority than the prefix ones (only exact match locations with the = modifier have greater priority than the regex ones). Moreover, you should not try to do it with the additional location block - all that you can achieve with that approach is the endless loop. Try this instead:

location ~ \.php$ {
    rewrite ^/admin/users.php$ /admin/users.php?sort_by=id&dir=ASC&lim=2&flt=1 break;
    include fastcgi_params;
    ...
}

Update

You have two rewrite directives in your PHP handler location. The first one will truncate the /admin prefix from any URI that start with that prefix. The second one will add additional query arguments to /admin/users.php URI, but it will be already rewritten to the /users.php. Moreover both rewrite directives have a break flag which means that if the first rule will be executed, the second one will never be reached. To combine both rules, try the following preserving the rewrite directives order:

rewrite ^/admin/users.php$ /users.php?sort_by=id&dir=ASC&lim=2&flt=1 break;
rewrite ^/admin/?(.*) /$1 break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文