打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
nginx rewrite arg 带问号的地址转发参数处理?Nginx重定向的参数问题
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.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;
}

发现问题:
重定向的地址都是 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.html permanent;
}
    这样重定向后发现 当输入/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;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
详解Nginx中的重定向功能
Nginx中rewrite实现二级域名、三级域名、泛域名、路径的重写
16、Nginx-重写与重定向
nginx的rewrite 参数和例子
nginx配置基础之rewrite
Apache Rewrite url重定向功能的简单配置
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服