找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1171|回复: 0

Nginx Rewrite重写技术

[复制链接]

296

主题

38

回帖

1274

积分

管理员

积分
1274
发表于 2021-4-16 17:41:53 | 显示全部楼层 |阅读模式
#Rewrite语法:
rewrite {规则} {定向路径} {重写类型}
rewrite ^/(.*) http://www.test.com/$1 permanent;

#Rewrite重写类型:
last                       相当于Apache里的[L]标记,表示完成rewrite
break           本条规则匹配完成后,终止匹配,不再匹配后面的规则
redirect        返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent       返回301永久重定向,浏览器地址会显示跳转后URL地址

#Rewrite实例:
1.http://192.168.2.1/1024.html -> http://192.168.2.1/index.php?id=1024
location / {
    rewrite ^/(\d+)\.html$ /index.php?id=$1 redirect;
    rewrite ^/(\d+)\.html$ /index.php?id=$1 break;
}

curl http://192.168.2.1/1024.html -L

2.http://192.168.2.1/home/1024.html -> http://192.168.2.1/home/index.php?id=1024
location / {
        rewrite ^/home/(\d+)\.html$ /home/index.php?id=$1 redirect;
}

3.http://192.168.2.1/admin/1024.html -> http://192.168.2.1/admin/index.php?id=1024
location / {
        rewrite ^/(\w+)/(\d+)\.html$ /$1/index.php?id=$2 redirect;
}

4.http://192.168.2.1/home/12-31-2020.html -> http://192.168.2.1/home/index.php?id=2020-12-31
location / {
        rewrite ^/(\w+)/(\d+)-(\d+)-(\d+)\.html$ /$1/index.php?id=$3-$1-$2 redirect;
}

5.http://192.168.2.1 -> http://www.baidu.com
location / {
        rewrite .* http://www.baidu.com permanent;
}

6.http://192.168.2.1:80 -> http://192.168.2.1:443
location / {
        if ($server_port ~* 80) {
                rewrite .* http://192.168.2.1:443 permanent;
                rewrite .* http://$host:443 permanent;
        }
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|外汇论坛 ( 粤ICP备16021788号 )

GMT+8, 2024-5-18 16:06 , Processed in 0.081596 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表