nginx rewrite arg 带问号的地址转发参数处理?Nginx重定向的参数问题

本文详细介绍了Nginx重定向规则中处理参数的问题及解决方法,包括如何移除多余的参数和保留特定参数的操作,通过实例演示了如何在重定向规则中加入或忽略查询字符串参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    Nginx重定向的参数问题

   在给某网站写rewrite重定向规则时,碰到了这个关于重定向的参数处理问题。默认的情况下,Nginx在进行rewrite后都会自动添加上旧地址中的参数部分,而这对于重定向到的新地址来说可能是多余。虽然这也不会对重定向的页面显示结果造成多少影响,但当你注意到新地址中包含有多余的“?xxx=xxx”时,心里总还是会觉得不爽。而且可能影响到网站的搜索优化SEO。那么该如何来处理这部分的内容呢?看了下面两个简单的例子你就会明白了。

例如:
把http://example.com/test.php?id=xxx 重定向到http://example.com/xxx.html
把我郁闷了好久,最后在谷大神那里找到了一片文章解决了。

这是刚开始的规则:
if ($query_string ~* "id=(\d+)$") {
       set $id $1;
       rewrite /art_list\.php/article/category-$id.html permanent;
       rewrite ^/goods\.php /goods/$id.htmlpermanent;
       rewrite ^/category\.php /products/$id.htmlpermanent;
       rewrite ^/child_cat\.php/category/$id.html  permanent;
       rewrite ^/art\.php /article/$id.htmlpermanent;
       rewrite ^/art_list\.php/article/category-$id.html permanent;
       rewrite ^/artid\.php /help/$id.html permanent;
       rewrite ^/article\.php /affiche/$id.htmlpermanent;
}

发现问题:
重定向的地址都是 xxx.html?id=xxx
最后我修改了参数:
if ($query_string ~* "id=(\d+)$") {
       set $id $1;
       rewrite /art_list\.php/article/category-$id.html? permanent;
       rewrite ^/goods\.php /goods/$id.html?permanent;
       rewrite ^/category\.php /products/$id.html?permanent;
       rewrite ^/child_cat\.php/category/$id.html?  permanent;
       rewrite ^/art\.php /article/$id.html?permanent;
       rewrite ^/art_list\.php/article/category-$id.html? permanent;
       rewrite ^/artid\.php /help/$id.html? permanent;
       rewrite ^/article\.php /affiche/$id.html?permanent;
}
结果正常了。

   注意,关键点就在于“?”这个尾缀。重定向的目标地址结尾处如果加了?号,则不会再转发传递过来原地址的问号?后面的参数那部分。
   假如又想保留某个特定的参数,那又该如何呢?可以利用Nginx本身就带有的$arg_PARAMETER参数自行补充来实现。
例如:
把http://example.com/test.php?para=xxx&p=xx 重写向到http://example.com/new.php?p=xx
可以写成:rewrite  ^/test.php /new.php?p=$arg_p?  permanent;

[全文结束]

参考文章:
   原始的动态页面需要给个301永久重定向到静态页面上,以告诉搜索引擎将原始的页面的权重转到新的静态页面下。
if ($query_string ~* "id=(\d+)$") {
       set $id $1;
       rewrite ^/goods\.php /goods/$id.htmlpermanent;
}
    这样重定向后发现当输入/goods.php?id=254访问的时候会跳转到/goods/254.html?id=254下,
   后面看见搜索引擎的收录地址也添加了后面不必要的参数,必须去掉后面参数。那该怎么来处理呢?
例如:
把http://examplecom/test.php?para=xxx重定向到http://examplecom/new
若按照默认的写法:rewrite ^/test.php(.*) /new permanent;
重定向后的结果是:http://examplecom/new?para=xxx
如果改写成:rewrite ^/test.php(.*) /new? permanent;
那结果就是:http://examplecom/new
   所以,关键点就在于“?”这个尾缀。假如又想保留某个特定的参数,那又该如何呢?可以利用Nginx本身就带有的$arg_PARAMETER参数来实现。
例如:
   把http://examplecom/test.php?para=xxx&p=xx重写向到http://examplecom/new?p=xx
   可以写成:rewrite  ^/test.php /new?p=$arg_p?  permanent;
来源:http://www.lc365.net/blog/b/15359/
PS:关于判断获取参数
if ($query_string ~* "appId=(\d+)$") {
  set $id $1;
  rewrite^/show/index/.*  /rewrite/htmlrewrite/$id.html;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值