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;

nginx rewrite arg 带问号的地址转发参数处理?Nginx重定向的参数问题的更多相关文章

  1. nginx根据项目名实现内网地址转发

    nginx根据访问的项目名进行内网地址转发 以下是nginx的配置信息: server { listen 8081; server_name localhost; #charset koi8-r; # ...

  2. Nginx Rewrite规则记录

    Rewrite 是一种服务器的重写脉冲技术,它可以使得服务器可以支持 URL 重写,是一种最新流行的服务器技术.它还可以实现限制特定IP访问网站的功能.很多情况下,某个 IP 的访问很容易造成 CPU ...

  3. nginx配置url中带问号的rewrite跳转

    今天收到一个需求,要将一个带查询参数的url跳转到另外一个静态url,安装常规的rewrite规则,如: rewrite ^/a.html?id=67$ http://zt.epython.cn/20 ...

  4. nginx rewrite (转发)

    1.location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所 ...

  5. 利用Nginx rewrite规则实现域名显性转发

    体验更优排版请移步原文:http://blog.kwin.wang/website/nginx-rewrite-realize-domain-forward.html 自己的blog域名最开始用的vb ...

  6. Nginx Rewrite规则

    location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配 ...

  7. [转帖]Nginx rewrite模块深入浅出详解

    Nginx rewrite模块深入浅出详解 https://www.cnblogs.com/beyang/p/7832460.html rewrite模块(ngx_http_rewrite_modul ...

  8. Nginx rewrite模块深入浅出详解

    rewrite模块(ngx_http_rewrite_module) nginx通过ngx_http_rewrite_module模块支持url重写.支持if条件判断,但不支持else.另外该模块需要 ...

  9. 【Web】Nginx Rewrite规则

    Rewrite介绍 Rewrite主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Nginx的Rewrite功能,在编译Ngi ...

随机推荐

  1. CSU-2019 Fleecing the Raffle

    CSU-2019 Fleecing the Raffle Description A tremendously exciting raffle is being held, with some tre ...

  2. c++矩阵

    这里讲的矩阵有创建矩阵,矩阵加法,矩阵乘法,输出矩阵这些功能. #include<iostream> using namespace std; template<class T> ...

  3. ABP 未能加载文件或程序集“System.ComponentModel.Annota, Version=4.2.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。

    切换System.ComponentModel.Annotations版本到4.4.1  重新编译即可

  4. 【Luogu】P3320寻宝游戏(Splay)

    题目链接 其实这题用Set就完事了但我不会Set 智商-=inf 求虚树上所有边权和的两倍. 具体方式就是splay把所有在虚树上的点存一下,(按照DFS序排序的)每次插入/删除会更新前驱和它.后继和 ...

  5. spring 解决中文乱码问题

    spring 解决中文乱码问题 使用spring的前提下在web.xml中配置 <filter> <filter-name>encodingFilter</filter- ...

  6. GDOI2018前夕 错误总结

    算法易错点 $FFT$ 1.注意精度,以及是否取整 2.注意$complex$类不要写错,复数乘法是这样的: complex operator *(const complex &b){retu ...

  7. shell if 条件语句实践

    对于if 语法 我们不过多做介绍,这里直接上实例,以开发rsync服务启动脚本为例,先对rsync做个简单介绍 [root@backup ~]# rpm -qa|grep rsync rsync--. ...

  8. 【05】react 之 组件state

    1.1.  状态理解 React的数据流:由父节点传递到子节点(由外到内传递),如果顶层组件某个prop改变了,React会向下传递,重新渲染所有使用过该属性的组件.除此之外React 组件内部还具有 ...

  9. About chrysanthemum and matrimony vine

    红枣.枸杞茶:可经常喝. 菊花茶.枸杞茶:适当,一般建议喝3天停3天会比较妥当. 湿热重的人不宜食红枣,可只泡枸杞.菊花或将红枣改为红茶,也能起到明目.养肝.益血.抗疲劳的功效.红枣要去核,才不会太燥 ...

  10. 通过命令编译的项目 导入 到eclipse

    通过命令编译的项目 导入 到eclipse后,需要 1.设置 sdk和ndk 的路径 2.设置 AndroidManifest.xml 中的版本为当前版本:<uses-sdk android:m ...