带查询字符串的NGINX重定向URL [英] NGINX Redirect URL with Query Strings

查看:62
本文介绍了带查询字符串的NGINX重定向URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个NGINX配置:

I have this NGINX configuration:

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"

Now, I want to redirect url something like "domain.com/index.php?tag=1&section=2&type=3" to "domain.com/tag/section/type"

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

how can I do that, where should I put the code? please help,

谢谢

我已经尝试过:

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

但是没用.

推荐答案

rewrite location 指令使用不包含查询字符串的规范化URI.

The rewrite and location directives use a normalized URI which does not include the query string.

要测试查询字符串,您需要使用 if 语句和/或 map 指令.

To test the query string, you will need to consult the $request_uri or $args variable using an if statement and/or map directive.

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

The advantage of using $request_uri is that it contains the original request and will help to avoid a redirection loop.

如果仅执行一种重定向,则 map 解决方案可能会超载.

If you only have one redirection to perform, the map solution is probably overfill.

尝试:

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

有关使用<的信息,请参见此警告代码>如果.

See this caution on the use of if.

这篇关于带查询字符串的NGINX重定向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆