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;
}
来源:https://www.cnblogs.com/xmtxh/p/12370166.html