zoukankan      html  css  js  c++  java
  • rewrite

    Rewrite基本概述
    1.Rewrite
    Rewrite即URL重写,主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他URL的过程。

    2.Rewrite使用场景
    1).URL地址跳转,例如用户访问old.com将其跳转到oldboy.com,或者当用户通过http的方式访问old.com时,将其跳转至https的方式
    访问oldboy.com
    2).URL伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同shi减少动态URL地址对外暴露过多的参数,提升
    更高的安全性。
    3).搜索引擎SEO优化依赖于URL路径,以便支持搜索引擎录入。
    4).可以调整用户浏览的URL,看起来更规范,合乎开发及产品人员的需求。

    安装chrom的http status 插件

    Rewrite配置语法

    Syntax:    rewrite regex replacement [flag];
    Default:    —
    Context:    server, location, if
    例:http://localhost:88/test1/test2/test.php
    $host: localhost
    $server_port: 80
    $request_uri: http://localhost:88/test1/test2/test.php
    $document_uri: /test1/test2/test.php
    $document_root: /var/www/html
    $request_filename: /var/www/html/test1/test2/test.php

    Rewrite匹配优先级

    1.执行server块的rewrite指令
    2.执行location匹配
    3.执行选定的location中的rewrite

    开启Nginx的Rewrite

    vim /etc/nginx/nginx.conf
    #1.设置nginx的错误日志级别为notice
    error_log /var/log/nginx/error.log notice;
    
    #2.在http模块层,增加一行rewrite_log日志
    http {
    ......
    rewrite_log on;
    ......
    }

    例1:访问blog.xiao.com则跳转至www.baidu.com

    vim wordpress.conf 
    server {
            listen 80;
            server_name blog.xiao.com;
            root /code/wordpress;
            index index.php;
            
            location / {
                    rewrite ^/  https://www.baidu.com;
            }
    
            location ~ .php$ {
                    root /code/wordpress;
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include fastcgi_params;
            }
    }

    例2: 用户访问/abc/1.html实际上真实访问/ccc/bbb/2.html

    配置一:
    [root@web01 conf.d]# cat rewrite.conf 
    server {
        listen 80;
        server_name rewrite.xiao.com;
        root /code;
        index index.html;
        
        location ~* /abc/1.html {
            rewrite /abc/1.html /bbb/ccc/index.html;
        }
    }
    
    配置优化一:
    [root@web01 conf.d]# cat rewrite.conf 
    server {
        listen 80;
        server_name rewrite.xiao.com;
        root /code;
        index index.html;
        
        location ~* /abc/1.html {
            rewrite .* /bbb/ccc/index.html;
        }
    }
    配置优化二:
    server {
            listen 80;
            server_name rewrite.xiao.com;
            root /code;
            index index.html;
    
            location ~* /abc/1.html {
                    #这两行都是302跳转,不要一起配置
                    #rewrite .* /bbb/ccc/index.html redirect;
                    return 302 /bbb/ccc/index.html;
            }
    }

    例3: 用户访问/2018/ccc/bbb/2.html实际真实访问是/2014/ccc/bbb/2.html

    # http://rewrite.xiao.com/2018/ccc/bbb/2.html =》http://rewrite.xiao.com/2014/ccc/bbb/2.html
    
    1.准备真实的目录及文件
    [root@web01 conf.d]# mkdir -p /code/2014/ccc/bbb
    [root@web01 conf.d]# echo "2014_bbb_ccc" > /code/2014/ccc/bbb/2.html
    
    2.添加rewrite的Nginx的配置文件
    
    [root@web01 conf.d]# cat rewrite.conf 
    server {
        listen 80;
        server_name rewrite.xiao.com;
        root /code;
        index index.html;
        
        location /2018 {
            rewrite ^/2018/(.*)$ /2014/$1 redirect; 
        }
    }

    例4:用户访问/test目录下任何内容,实际上真实访问是https://www.baidu.com

    location /test {
        rewrite (.*) https://www.baidu.com redirect;
    }
    
    泛域名
    *.xxx.com

    例5: 用户访问course-11-22-33.html实际上真实访问是/course/11/22/33/course_33.html

    # http://www.xiao.com/course-11-22-33.html ==》http://www.xiao.com/course/11/22/33/course_33.html
    
    1.准备真实的目录及文件
    [root@web01 conf.d]# mkdir -p /code/course/11/22/33
    [root@web01 conf.d]# echo "course 112233" > /code/course/11/22/33/course_33.html
    
    2.添加rewrite的Nginx的配置文件
    [root@web01 conf.d]# cat rewrite.conf 
    server {
        listen 80;
        server_name rewrite.xiao.com;
        root /code;
        index index.html;
        
        location / {
            #灵活
            rewrite ^/(.*)-(.*)-(.*)-(.*).html$ /$1/$2/$3/$4/$1_$4.html redirect;
            #固定
            #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
        }    
    }

    例6:将http请求,跳转至https

    server {
            listen 80;
            server_name xiao.com;
            rewrite ^(.*) https://$server_name$1 redirect;
            #return 302 https://$request_uri;
    }
    
    server {
            listen 443;
            server_name xiao.com;
            ssl on;
    }

     Rewrite标记Flag

    rewrite指令根据表达式来重定向URL,或者修改字符串。可以应用于server,location,if环境下,每行rewrite指令最后跟一个flag标记,
    支持的flag标记如下表格所示:

    flag
    last      本条规则匹配完成后,停止匹配,不在匹配后面的规则
    break     本条规则匹配完成后,停止匹配,继续匹配后面的规则
    redirect  返回302临时重定向,地址栏会显示跳转后的地址
    permanent 返回301永久重定向,地址栏会显示跳转后的地址

     last与break对比总结

    last
    1.last进行Rewrite匹配成功,停止当前这个请求,并根据rewrite匹配的规则重新向Server发起一个请求。
    2.新请求的内容是域名+URL,对应的地址为www.xiao.com/2017-old
    break
    1.break进行Rewrite匹配成功后,不会像last重新发起请求,首先查找站点目录下/code/2017_new/默认返回页是否存在,如不存在则404,如存在
    继续往下匹配
    2.根据Rewrite匹配的规则,跳转至www.xiao.com/2017_old/的URL地址,匹配成功后则不继续匹配
    [root@web01 conf.d]# cat rewrite.conf 
    server {
        listen 80;
        server_name rewrite.xiao.com;
        root /code;
        index index.html;
        
        location ~ ^/2019_new {
            rewrite ^/2019_new /2017_old break;
        }    
        
        location ~ ^/2019_new {
            rewrite ^/2019_new /bbb/ccc/index.html redirect;
        }
    
        location ~ ^/2020_new {
            rewrite ^/2020_new /2017_old break;
        }
    
        location ~ /2017_old {
            index index.html;
        }
    }
  • 相关阅读:
    Visual Studio 调试系列3 断点
    mysql客户端(Navicat)远程登录操作遇到问题1142
    php Socket通信
    centos crontab(定时任务) 使用
    nginx中配置pathinfo模式示例
    IE9总是弹出“ICBC Anti-Phishing class” 加载项是否要启用还是不启用的提示
    windows 2008 R2 断电重启进入修复模式
    unserialize() [function.unserialize]: Error at offset
    解决子级用css float浮动 而父级div没高度不能自适应高度
    php追加编译GD库
  • 原文地址:https://www.cnblogs.com/xmtxh/p/12370166.html
Copyright © 2011-2022 走看看