Nginx Rewrite重写技术
#Rewrite语法:rewrite {规则} {定向路径} {重写类型}
rewrite ^/(.*) http://www.test.com/$1 permanent;
#Rewrite重写类型:
last 相当于Apache里的标记,表示完成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;
}
}
页:
[1]