何为地址重写,域名如何进行重定向

地址重写,获取一个来访的URL请求,然后改写成服务器可以处理的另一个URL的过程

功能:www.baidu.com——>https://www.baidu.com

优点:缩短URL,隐藏实际路径,提高安全性;

易于用户记忆和键入;易于被搜索引擎收录

常见网站应用场景:

当网站文件移动或文件目录名称发现改变,出于SEO需要,你需要保持旧的URL

网站改版了,网站导航和链接发生变化,为了继续持有原链接带来的流量,需要保持旧的URL

rewrite语法:rewrite 旧地址(正则regex)  新地址(跳转后的URL)【选项flag】

if (条件){…}

[选项flag]:

last:停止执行其他重写规则,根据URL继续搜索其他location,地址栏不改变

break:停止执行其他重写规则,完成本次请求

redirect:302临时重定向,地址栏改变,爬虫不更新URL

permament:301永久重定向,地址栏改变,爬虫更新URL

正则表达式匹配模式

区分大小写匹配:~

不区分大小写匹配:~*

区分大小写不匹配:!~

不区分大小写不匹配:!~*

判断文件是否存在:-f

判断目录是否存在:-d

判断文件是否可执行:-x

判断文件、目录、连接是否存在:-e

页面跳转:访问a.html—–>自动跳转到b.html   /usr/local/nginx/conf/nginx.conf

server {listen 80

server_name localhost;

location / {root html;

index index.html index.htm;

rewrite /a.html /b.html  redirect;}}   //选项redirect可有可无,区别为有选项时地址栏改变,不使用此选项则地址栏不变

服务端Proxy  # echo "BB" > /usr/local/nginx/html/b.html

# /usr/local/nginx/sbin/nginx  -s  reload

客户端client  # firefox http://192.168.4.25/a.html

网站跳转:访问192.168.4.5跳转到www.tmooc.cn   /usr/local/nginx/conf/nginx.conf

注:写在location内对根文件夹生效,写在location外对所有文件生效

seerver {listen 80;

server _ name localhost;

rewrite ^/ http:www.tmooc.cn;}             //^/也可写成^/.*,模糊匹配

location / {root html;

index  index.html index.htm;}}

服务端Proxy   # /usr/local/nginx/sbin/nginx  -s  reload

客户端client  # firefox http://192.168.4.25

附加:访问旧的网站/下面子页面,跳转到新的网站/下相同页面

rewrite ^/(.*) http://www.jd.com/$1;     //(.*) 保留和复制;$1在nginx代表粘贴

/usr/local/nginx/conf/nginx.conf

server {listen       80;

server_name  localhost;

rewrite ^/(.*) http://www.tmooc.cn/$1;            //在location外部

location / {root   html;

index  index.html index.htm;}}

服务端Proxy   # /usr/local/nginx/sbin/nginx  -s  reload

客户端client # firefox  http://192.168.4.5

www.tarena.com—>bbs.tarena.com    /usr/local/nginx/conf/nginx.conf

seerver {listen 80;

server _ name www.tarena.com;

location / {root html;

index index.html index.htm;

rewrite ^/(.*) http://bbs.tarena.com/$1;}}   //在location内部

*.jpg或*.gif —-> logo.png        /usr/local/nginx/conf/nginx.conf

location / {root html;

index index.html index.htm;

rewrite \.(gif|jpg)$ /logo.png;}

访问不同浏览器的相同链接返回不同的页面     /usr/local/nginx/conf/nginx.conf

注:if($http_user_agent ~*  浏览器类型(firefox/uc/…..)){

rewrite  ^/(.*)   /浏览器类型文件夹/文件;}

写在location内是死循环(报错http500),写在location外是正常

在服务端Proxy创建网页目录以及对应的页面文件:

# echo "I am Normal page" > /usr/local/nginx/html/test.html  //正常页面

# mkdir  -p  /usr/local/nginx/html/curl/

# echo "I am is curl page" > /usr/local/nginx/html/curl/test.html

# cp /usr/share/backgrounds/gnome/Road.jpg > /usr/local/nginx/curl/test.jpg

修改配置文件

seerver {listen 80;

server _ name www.tarena.com;

location / {root html;

index index.html index.htm;}

if ($http_user_agent ~* curl) {                   //识别客户端curl浏览器

rewrite ^/(.*) /curl/$1 break;}}

服务端Proxy   # /usr/local/nginx/sbin/nginx  -s  reload

客户端client # firefox  http://192.168.4.5/test.html

# curl     http://192.168.4.5/test.html

# curl     http://192.168.4.5/test.jsp

访问资源不存在时,重定向到指定页面       /usr/local/nginx/conf/nginx.conf

注:if(!-e  $request_filename){

rewrite  ^/  http://域名/文件;}

seerver {listen 80;

server _ name www.tarena.com;

location / {root html;

index index.html index.htm;}

if(! -e $request_filename){

rewrite ^/http://www.tarena.com/none.html;}}

服务端Proxy   # /usr/local/nginx/sbin/nginx  -s  reload

客户端client # firefox  http://192.168.4.5/a.mp4

你也许会喜欢如下的文章?

(责任编辑:365天天网)