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;
nginx rewrite arg 带问号的地址转发参数处理?Nginx重定向的参数问题的更多相关文章
- nginx根据项目名实现内网地址转发
nginx根据访问的项目名进行内网地址转发 以下是nginx的配置信息: server { listen 8081; server_name localhost; #charset koi8-r; # ...
- Nginx Rewrite规则记录
Rewrite 是一种服务器的重写脉冲技术,它可以使得服务器可以支持 URL 重写,是一种最新流行的服务器技术.它还可以实现限制特定IP访问网站的功能.很多情况下,某个 IP 的访问很容易造成 CPU ...
- nginx配置url中带问号的rewrite跳转
今天收到一个需求,要将一个带查询参数的url跳转到另外一个静态url,安装常规的rewrite规则,如: rewrite ^/a.html?id=67$ http://zt.epython.cn/20 ...
- nginx rewrite (转发)
1.location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所 ...
- 利用Nginx rewrite规则实现域名显性转发
体验更优排版请移步原文:http://blog.kwin.wang/website/nginx-rewrite-realize-domain-forward.html 自己的blog域名最开始用的vb ...
- Nginx Rewrite规则
location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配 ...
- [转帖]Nginx rewrite模块深入浅出详解
Nginx rewrite模块深入浅出详解 https://www.cnblogs.com/beyang/p/7832460.html rewrite模块(ngx_http_rewrite_modul ...
- Nginx rewrite模块深入浅出详解
rewrite模块(ngx_http_rewrite_module) nginx通过ngx_http_rewrite_module模块支持url重写.支持if条件判断,但不支持else.另外该模块需要 ...
- 【Web】Nginx Rewrite规则
Rewrite介绍 Rewrite主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Nginx的Rewrite功能,在编译Ngi ...
随机推荐
- TOJ 3974: Region n条直线m个圆最多将圆分为几个区域
3974: Region Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 33 ...
- Map 中的EntrySet() ,Map的遍历
我们循环Map时一般用到EntrySet(),EntrySet() 返回的时Set集合(Set<Map.Entry<K, V>>). 那么这里的有Map.Entry< ...
- Box 类
public class Box extends JComponent implements Accessible使用 BoxLayout 对象作为其布局管理器的一个轻量级容器.Box 提供几个对使用 ...
- 【VBA】Do While……Loop循环,遍历某列
[说明] Do While……Loop循环,遍历某列 i = Do While Trim(T_sh.Cells(i, NOTESID_COL)) <> "" If T_ ...
- Java项目性能监控和调优工具-Javamelody的学习总结
1.简介: JavaMelody能够在运行环境监测Java或Java EE应用程序服务器.并以图表的形式显示:Java内存和Java CPU使用情况,用户Session数量,JDBC连接数,和http ...
- Python之面向对象:继承
概念:子类继承父类的属性和方法. 一个派生类(derived class)继承基类(bass class)字段和方法.继承也允许把一个派生类的对象作为一个基类对象对待. 一.单继承 :推崇.特点和使用 ...
- mongodb使用1
首先官网下载mongodb放在根目录下.新建db文件夹,在命令行中进入bin路径,然后运行mongod开启命令,同时用--dbpath指定数据存放地点为“db”文件夹 mongod --dbpath= ...
- 安装最新版本的cocoapods
因为公司的iOS项目使用了cocoapods来管理第三方库,所以要求所有组员的cocoapods版本一致.一般的就是执行: $ sudo gem install -n /usr/local/bin c ...
- 向量内积(bzoj 3243)
Description 两个d 维向量A=[a1,a2,...,ad]与B=[b1,b2,...,bd]的内积为其相对应维度的权值的乘积和,即: 现有 n 个d 维向量x1,...,xn ,小喵喵想知 ...
- pat 甲级 Public Bike Management
Public Bike Management (30) 题目描述 There is a public bike service in Hangzhou City which provides grea ...