nginx带参数跳转


原链接:https://www.baidu.com/benefit_detail?slug=bankofchina-20170320
目标链接:https://www.test.cn/boc
location ~ /benefit_detail {
if ($args ~* "slug=bankofchina-20170320") {
rewrite ^/benefit_detail /boc? permanent;
}
try_files $uri $uri/ /index.php?$query_string;
}

常见跳转事例:
1,将www.myweb.com/connect 跳转到connect.myweb.com

rewrite ^/connect$ http://connect.myweb.com permanent;

rewrite ^/connect/(.*)$ http://connect.myweb.com/$1 permanent;

2,将connect.myweb.com 301跳转到www.myweb.com/connect/ 

if ($host = "connect.myweb.com"){

rewrite ^/(.*)$ http://www.myweb.com/connect/$1 permanent;

    }

3,myweb.com 跳转到www.myweb.com

if ($host != ‘www.myweb.com‘ ) { 

rewrite ^/(.*)$ http://www.myweb.com/$1 permanent; 

    }

4,www.myweb.com/category/123.html 跳转为 category/?cd=123

rewrite "/category/(.*).html$" /category/?cd=$1 last;

5,www.myweb.com/admin/ 下跳转为www.myweb.com/admin/index.php?s=

if (!-e $request_filename){

rewrite ^/admin/(.*)$ /admin/index.php?s=/$1 last;

    }

6,在后面添加/index.php?s=

if (!-e $request_filename){

    rewrite ^/(.*)$ /index.php?s=/$1 last;

    }

7,www.myweb.com/xinwen/123.html  等xinwen下面数字+html的链接跳转为404

rewrite ^/xinwen/([0-9]+)\.html$ /404.html last;

8,http://www.myweb.com/news/radaier.html 301跳转 http://www.myweb.com/strategy/

rewrite ^/news/radaier.html http://www.myweb.com/strategy/ permanent;

9,重定向 链接为404页面

rewrite http://www.myweb.com/123/456.php /404.html last;

10, 禁止htaccess

location ~//.ht {

         deny all;

     }

11, 可以禁止/data/下多级目录下.log.txt等请求;

location ~ ^/data {

     deny all;

     }

12, 禁止单个文件

location ~ /www/log/123.log {

      deny all;

     }

13, http://www.myweb.com/news/activies/2014-08-26/123.html 跳转为 http://www.myweb.com/news/activies/123.html

rewrite ^/news/activies/2014\-([0-9]+)\-([0-9]+)/(.*)$ http://www.myweb.com/news/activies/$3 permanent;

14,nginx多条件重定向rewrite

如果需要打开带有play的链接就跳转到play,不过/admin/play这个不能跳转

        if ($request_filename ~ (.*)/play){ set $payvar ‘1‘;}
        if ($request_filename ~ (.*)/admin){ set $payvar ‘0‘;}
        if ($payvar ~ ‘1‘){
                rewrite ^/ http://play.myweb.com/ break;
        }

15,http://www.myweb.com/?gid=6 跳转为http://www.myweb.com/123.html

 if ($request_uri ~ "/\?gid\=6"){return  http://www.myweb.com/123.html;}

正则表达式匹配,其中:

* ~ 为区分大小写匹配

* ~* 为不区分大小写匹配

* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配

文件及目录匹配,其中:

* -f和!-f用来判断是否存在文件

* -d和!-d用来判断是否存在目录

* -e和!-e用来判断是否存在文件或目录

* -x和!-x用来判断文件是否可执行

flag标记有:

* last 相当于Apache里的[L]标记,表示完成rewrite

* break 终止匹配, 不再匹配后面的规则

* redirect 返回302临时重定向 地址栏会显示跳转后的地址

* permanent 返回301永久重定向 地址栏会显示跳转后的地址
nginx各个内置变量含义请参考:
https://blog.csdn.net/wanglei_storage/article/details/66004933
【nginx try_files的理解】
以 try_files $uri $uri/ /index.php; 为例,当用户请求 http://servers.blog.ustc.edu.cn/example 时,
这里的 $uri 就是 /example。try_files 会到硬盘里尝试找这个文件。如果存在名为 /$root/example(其中 $root 是 WordPress
的安装目录)的文件,就直接把这个文件的内容发送给用户。显然,目录中没有叫 example 的
文件。然后就看 $uri/,增加了一个 /,也就是看有没有名为 /$root/example/ 的目录。又找不到,
就会 fall back 到 try_files 的最后一个选项 /index.php,发起一个内部 “子请求”,也就是相当于
nginx 发起一个 HTTP 请求到 http://servers.blog.ustc.edu.cn/index.php。这个请求会被 location
~ \.php$ { ... } catch 住,也就是进入 FastCGI 的处理程序。而具体的 URI 及参数是在 REQUEST_URI
中传递给 FastCGI 和 WordPress 程序的,因此不受 URI 变化的影响
[$request_uri解释]
$request_uri就是完整url中刨去最前面$host剩下的部分,比如http://www.baidu.com/pan/beta/test1?fid=3这个url,
去掉www.baidu.com剩下的就是了,日志里会看到打印出来的$request_uri其实是/pan/beta/test1?fid=3。
如果只访问www.baidu.com,$request_uri里也会有个/的。

if ($request_uri ~* "^/$") 表示url中只有域名,后面不跟任何东西,比如www.baidu.com。
if ($request_uri ~* "test") 表示域名后面那串儿只要包含test这个关键词,就可匹配成功,
比如www.baidu.com/pan/beta/test3

if ($request_uri ~* "^/$"){
               rewrite ^ http://kj.fnwtv.com/index.html permanent;
           }

if ($request_uri !~* "^/$") {
                rewrite ^ http://www.fnwtv.com/ permanent;
            }
【Nginx if 条件判断参考】
https://www.cnblogs.com/saneri/p/6257188.html
location proxy_pass 后面的url 加与不加/的区别参考:
https://blog.51cto.com/huangzp/1954575

原文地址:https://blog.51cto.com/11873474/2464044

时间: 2025-01-17 20:44:15

nginx带参数跳转的相关文章

jquery mobile 带参数跳转收集(紧个人使用,测试完会补全)

//临时存储 var TempCache = { cache:function(value){ localStorage.setItem("EasyWayTempCache",value); }, getCache:function(){ return localStorage.getItem("EasyWayTempCache"); }, setItem:function(key,value){ localStorage.setItem(key,value); }

nginx带端口跳转

输入10.0.51.100:5082/osp-mbr,没想到nginx跳转为10.0.51.100/osp-mbr/a,因此直接报404 在头里设置host端口号就可以了.

《小白滴滴系列》-angualr的带参数跳转

angualr跳转有两种方式(我知道的): 1.ui-sref:后面跟上状态名,有参数的话在状态名后面添加 <a ui-sref="TopicComments({ID:list.ID})">查看及回复</a><br /> 2.利用$state:参数分别是:状态的名字(注意是状态不是路由的名称),跳转的参数,跳转的时候刷新 $state.go('salaryDetail', { Time: paTime }, { reload: true }); 注意

获取URL值带参数跳转

//要跳转界面 api.openWin({ name: 'PayOrder', url: 'PayOrder.html?id=2', pageParam:{name:pr} }); //跳转后的界面 var url = location.search; //获取url中"?"符后的字串 var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs

小程序之带参数跳转到tab页

wx.switchTab({ url: '../../message/message/message', }) //wx.switchTab url不能带参数 解决方法?? (紫色表示非固定需要自己更改的) app.js ?? globalData: { activityid: '', //需要传递的参数 title: '' //需要传递的参数 } 传参页面.js ?? var app = getApp() issue: function(e){ app.globalData.activityi

微信小程序页面带参数跳转及接收参数内容navigator

功能从index页面跳转到draw页面,并在draw页面获取id及imgUrl index.wxml <navigator class='looks-view' wx:for="{{imgUrlNew}}" wx:key="index" url="/pages/draw/draw?id={{item.id}}&imgUrl={{item.img}}"> <image src='{{item.img}}' class='l

微信小程序页面带参数跳转

页面传递参数的方式 data-para js获取参数 原文地址:https://www.cnblogs.com/yxj9536/p/10329151.html

JS——页面带参数跳转

#index.html window.location.href = "../home.html?value=" + rec_value; #home.html <script> var a = GetRequest(); console.log(a); function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new Object(); if

springMVC带参数请求重定向

SpirngMVC返回逻辑视图名 可以分下面几种情况: 1. servlet进行请求转发,返回到jsp页面,如  return "index.jsp" ; 2. servlet 返回结果,让请求 重定向到某个jsp页面 ,此时servlet 返回语句类似:  return  " redirect : index.jsp "; 3. servlet 的返回结果是 请求另外一个servlet   此时servlet 返回语句类似:  return  " red