【问题标题】:NGINX Redirect URL with Query Strings带有查询字符串的 NGINX 重定向 URL
【发布时间】:2019-06-17 04:00:01
【问题描述】:

我有这个 NGINX 配置:

root    /var/www/web;
index   index.php;

server_name  domain.com;
access_log  off;
error_log on;

location / {
    rewrite ^/(.*)$ /index.php?tag=$1&page=1 last;
}

现在,我想重定向网址,例如 "domain.com/index.php?tag=1&section=2&type=3" 到 "domain.com/tag/section/type"

我该怎么做,我应该把代码放在哪里?请帮忙,

谢谢

我已经试过了:

location / {
   rewrite ^/index\.php?tag=(.*)&section=(.*)&type=(.*)$ /$1/$2/$3 permanent;
   rewrite ^/(.*)$ /index.php?tag=$1&page=1 last;
}

但它没有工作..

【问题讨论】:

  • 变量呢?我应该为重定向写什么?我试过了,还是不行。。

标签: linux nginx configuration webserver


【解决方案1】:

rewritelocation 指令使用不包含查询字符串的规范化 URI。

要测试查询字符串,您需要使用if 语句和/或map 指令查询$request_uri$args 变量。

使用$request_uri 的优点是它包含原始请求,有助于避免重定向循环。

如果您只有一个重定向要执行,map 解决方案可能会溢出。

试试:

if ($request_uri ~ ^/index\.php\?tag=(.*)&section=(.*)&type=(.*)$) {
    return 301 /$1/$2/$3;
}
location / {
    ...
}

请参阅this caution 了解if 的使用。

【讨论】:

  • 谢谢,这正是我正在寻找的......我刚刚意识到应该将代码放在位置之外......
猜你喜欢
  • 2017-06-02
  • 2017-04-10
  • 1970-01-01
  • 2014-07-08
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
相关资源
最近更新 更多