URL重写

  • URL重写是指将一个URL请求重新写成网站可以处理的另一个URL的过程。如将http://www.demo.com/product?id=1重写成http://www.demo.com/product/1的样式,以便SEO更好的收录。

1. 指令语法

conf
1
2
3
server { 
    rewrite 规则 定向路径 重写类型; 
}
  • 规则 :可以使用正则或者字符串来表示相匹配的地址。
  • 定向路径 :表示匹配到规则后要定向的路径,如果规则里有正则,则可以使用$index来表示正则里的捕获分组
  • 重写类型 :
    • last:停止处理当前的rewrite指令集,而后通过重写后的规则重新发起请求,浏览器地址栏URL地址不变。
    • break:停止处理当前上下文中的其他重写模块指令 (直接生效并停止后续的匹配location)。
    • redirect:如果替换字符串不以“ http://”,“ https://”或“ $scheme” 开头,返回带有302代码的临时重定向,浏览器地址会显示跳转后的URL地址。
    • permanent:返回301代码的永久重定向,浏览器地址栏会显示跳转后的URL地址。

示例:

nginx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
server {
    # 访问 /last.html 的时候,页面内容重写到 /index.html 中
    rewrite /last.html /index.html last;

    # 访问 /break.html 的时候,页面内容重写到 /index.html 中,并停止后续的匹配
    rewrite /break.html /index.html break;

    # 访问 /redirect.html 的时候,页面直接302定向到 /index.html中
    rewrite /redirect.html /index.html redirect;

    # 访问 /permanent.html 的时候,页面直接301定向到 /index.html中
    rewrite /permanent.html /index.html permanent;

    # 把 /html/*.html => /post/*.html ,301定向
    rewrite ^/html/(.+?).html$ /post/$1.html permanent;

    # 把 /search/key => /search.html?keyword=key
    rewrite ^/search\/([^\/]+?)(\/|$) /search.html?keyword=$1 permanent;
}

2. 条件判断

  • 使用“ =”和“ !=”运算符比较变量和字符串;
  • 正则表达式匹配,*不区分大小写的匹配,!~区分大小写的不匹配

内置的条件判断:

  • -f/!-f用来判断是否存在文件
  • -d/!-d用来判断是否存在目录
  • -e/!-e用来判断是否存在文件或目录
  • -x/!-x用来判断文件是否可执行

内置的全局变量:

  • $args :这个变量等于请求行中的参数,同$query_string
  • $content_length : 请求头中的Content-length字段。
  • $content_type : 请求头中的Content-Type字段。
  • $document_root : 当前请求在root指令中指定的值。
  • $host : 请求主机头字段,否则为服务器名称。
  • $http_user_agent : 客户端agent信息
  • $http_cookie : 客户端cookie信息
  • $limit_rate : 这个变量可以限制连接速率。
  • $request_method : 客户端请求的动作,通常为GET或POST。
  • $remote_addr : 客户端的IP地址。
  • $remote_port : 客户端的端口。
  • $remote_user : 已经经过Auth Basic Module验证的用户名。
  • $request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。
  • $scheme : HTTP方法(如http,https)。
  • $server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
  • $server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。
  • $server_name : 服务器名称。
  • $server_port : 请求到达服务器的端口号。
  • $request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
  • $uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
  • $document_uri : 与$uri相同。

示例:

nginx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
server {
    # 如果文件不存在则返回400
    if (!-f $request_filename) {
        return 400;
    }

    # 如果host不是xuexb.com,则301到xuexb.com中
    if ( $host != "xuexb.com" ){
        rewrite ^/(.*)$ https://xuexb.com/$1 permanent;
    }

    # 如果请求类型不是POST则返回405
    if ($request_method = POST) {
        return 405;
    }

    # 如果参数中有 a=1 则301到指定域名
    if ($args ~ a=1) {
        rewrite ^ http://example.com/ permanent;
    }

    if ($http_user_agent ~ MSIE) {
        rewrite ^(.*)$ /msie/$1 break;
    }

    if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
        set $id $1;
    }
    
    # 访问 /test.html 时
    location = /test.html {
        # 默认值为xiaowu
        set $name xiaowu;

        # 如果参数中有 name=xx 则使用该值
        if ($args ~* name=(\w+?)(&|$)) {
            set $name $1;
        }

        # 301
        rewrite ^ /$name.html permanent;
    }
}

「真诚赞赏,手留余香」

Hollson码夫庄园

嗯,我的梦想是被读者的稿费包养~

使用微信扫描二维码完成支付

Hollson

评论