服务器重定向与客户端重定向学习,辨析与实现

1. 定义###

网域名称转址(英语:URL redirection,或称网址重定向或URL重定向),是指当用户浏览某个网址时,将他导向到另一个网址的技术


2. 作用###

  1. 相似域名

  2. 网站迁移至新域名

  3. 记录外送链接

例:QQ邮箱中点击邮件链接是的警告其实服务器在收到邮件时就以做了处理
  1. 简化网址
例:微博中的附加链接

3. 分类###

  1. 服务器端跳转(forward跳转)
1. 原理

  用户 *请求* -> 原服务器 *请求* -> 资源服务器 *响应* -> 用户

2. 特点

  - 重定向后用户的URL没发生任何变化

  - 只能进行站点内跳转

  - 根目录代表服务器跟路径webapps

  - request设置的属性在跳转后的页面仍可以使用

  - 使用<jsp:param name="参数名" value="参数值" />传递参数

  - 执行到跳转语句后马上无条件跳转,之后的代码不再执行(跳转之前一定要释放全部资源)
  1. 客户端跳转(response跳转)
1. 原理

  用户 *请求* -> 原服务器 *响应* -> 用户 *请求* -> 资源服务器 *响应* -> 用户

2. 特点

  - URL变为重定向后所请求的URL

  - 可以进行站外跳转

  - 根目录代表站点根路径

  - 跳转后的页面不能使用上一个页面的request属性

  - 使用地址重写传递参数(response.sendRedirect("URL?参数名=参数值"))    

  - 所有代码执行完毕后跳转

4. 实现###

  1. PHP

<?php

header('HTTP/1.1 301 Moved Permanently');

header('Location: http://www.example.com/');

exit();


<?php

header( 'Refresh: 0; url=http://www.example.com/' );

exit();

  1. Apache HTTP Server's mod_alias extension

Redirect permanent /oldpage.html http://www.example.com/newpage.html

Redirect 301 /oldpage.html http://www.example.com/newpage.html

  1. Apache mod_rewrite

RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^.:]+.)*oldsite.example.com.?(:[0-9]*)?$ [NC]

RewriteRule ^(.*)$ http://newsite.example.net/$1 [R=301,L]

  1. HTML的Refresh Meta tag and HTTP refresh header

<html>

    <head>

        <meta http-equiv="Refresh" content="0; url=http://www.example.com/" />

    </head>

    <body>

    </body>

</html>

  1. HTML的Frame redirects

<html>

    <frameset rows="100%">

      <frame src="http://www.example.com/">

    </frameset>

</html>

  1. JavaScript redirects

window.location='http://www.example.com/';

感谢读者的来访,希望这篇博客能够帮到你,持续关注请使用RSS订阅或者关注我的账号:)

本博客所有内容系作者原创,转载务必注明出处以对作者表示尊重!

热爱互联网的 二两丶

原文地址:https://www.cnblogs.com/erliang/p/4065716.html