1.案例一:用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html
#创建页面
[root@web01 ~]# mkdir /code/ccc/bbb/ -p
[root@web01 ~]# echo "/ccc/bbb/2.html" > /code/ccc/bbb/2.html
#配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
listen 80;
server_name nginx.rewrite.com;
root /code;
location ~* /abc {
rewrite ^(.*)$ /ccc/bbb/2.html redirect;
}
}
2.案例二:用户访问/2018/ccc/bbb/2.html
实际上真实访问的是/2014/bbb/ccc/2.html
[root@web01 ~]# mkdir /code/2014/bbb/ccc/ -p
[root@web01 ~]# echo "/2014/bbb/ccc/2.html" > /code/2014/bbb/ccc/2.html
#配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
listen 80;
server_name nginx.rewrite.com;
root /code;
location ~* ^/2018 {
rewrite ^/2018/(.*)/(.*)/(.*) /2014/$2/$1/$3 redirect;
}
}
3.案例三:用户访问course-11-22-33.html
实际上真实访问的是/course/11/22/33/course_33.html
[root@web01 ~]# mkdir /code/course/11/22/33/ -p
[root@web01 ~]# echo "/course/11/22/33/course_33.html" >/code/course/11/22/33/course_33.html
#配置
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
listen 80;
server_name nginx.rewrite.com;
root /code;
location ~* ^/course {
#灵活配法
rewrite ^/(.*)-(.*)-(.*)-(.*).html$ /$1/$2/$3/$4/$1_$4.html redirect;
#固定配法
#rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
}
}