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. Git 笔记 - section 1

    1. 创建版本库 创建目录 git init git add <file> git commit -m "本次提交内容的说明" 2. 查看状态 git status g ...

  2. 再次理解javascript的apply

    普通函数执行的时候,this指向函数执行的上下文  其实就是一个原型链的结构...    我一直没有搞懂原型链莫非它们像链条一样连在一起?    昂...   原型链可以理解成继承吗?   就像,ja ...

  3. 【转】一个java页游服务器框架

    源地址:http://www.cnblogs.com/metoy/p/4305326.html?utm_source=tuicool&utm_medium=referral 一.前言 此游戏服 ...

  4. Log4j官方文档翻译(五、日志输出的方法)

    日志类提供了很多方法用于处理日志活动,它不允许我们自己实例化一个logger,但是提供给我们两种静态方法获得logger对象: public static Logger getRootLogger() ...

  5. Codeforces 954E Water Taps

    题目大意 有 $n$($1\le n\le 200000$)个变量 $x_1, x_2, \dots, x_n$,满足 \begin{equation} 0\le x_i \le a_i \label ...

  6. 树上路径(path)

    树上路径(path) 题目描述 在Berland,有n个城堡. 每个城堡恰好属于一个领主.不同的城堡属于不同的领主.在所有领主中有一个是国王,其他的每个领主都直接隶属于另一位领主,并且间接隶属于国王. ...

  7. 刷题总结——Genghis Khan the Conqueror (hdu4126)

    题目: Genghis Khan(成吉思汗)(1162-1227), also known by his birth name Temujin(铁木真) and temple name Taizu(元 ...

  8. THUWC 2018(游记)

    这次是在雅礼洋湖中学举行的,一所2017年才创办的学校,新的学校, 貌似有些危险,积雪过多屋顶上的冰块砸下来,很容易砸到人, 听说最近就有一个人被砸死了. Day1 昨天睡的比较迟,12点吧,今天早上 ...

  9. iOS工程中一天只让进行一次的操作如何做?

     转至: iosNSDateNSObject一天一次   整体思路:当进行操作的时候记录操作时间存在偏好设置当中,当再次点击的时候获取现在的时间然后和之前记录的时间进行比较.如果是一天那么就提示“今天 ...

  10. CF126B password&&HDU 4763 Theme Section

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 http://codeforces.com/problemset/problem/126/B 这两个题都是 ...