zoukankan      html  css  js  c++  java
  • ISAPI Rewrite实现IIS 301转向

    Windows主机IIS下的301转向设置

    在IIS 管理后台 -> 选择你要重定向的文件或文件夹 -> 右键“重定向到URL” -> 输入需要转向的目标URL ->选择“资源的永久重定向”。

    另外,如果你的Windows虚拟主机空间支持ISAPI_Rewrite,那么在IIS下利用ISAPI_Rewrite不仅可以实现url 重写,还可以用来设置301转向,下面分别是三个版本的ISAPI_Rewrite对应的带www的域名301转向到不带www域名的代码:

    1. 将不带www的顶级域名301重定向到带www的域名

    # ISAPI_Rewrite 2.x 版本
    [ISAPI_Rewrite]
    # 3600 = 1 hour
    CacheClockRate 3600
    RepeatLimit 32
    RewriteCond Host: ^farlee.info$
    RewriteRule (.*) http://www.farlee.info$1 [I,RP]

    # ISAPI_Rewrite 3.0 版本
    [ISAPI_Rewrite]
    # 3600 = 1 hour
    CacheClockRate 3600
    RepeatLimit 32
    RewriteCond %{HTTP:Host} ^farlee.info$
    RewriteRule (.*) http://www.farlee.info$1 [NC,R=301]

    2. 不同域名之间的301转向

    # ISAPI_Rewrite 2.x 版本
    [ISAPI_Rewrite]
    # 3600 = 1 hour
    CacheClockRate 3600
    RepeatLimit 32
    RewriteCond %{HTTP:Host} ^isapirewrite.com$
    RewriteRule (.*) http://www.farlee.info$1 [NC,R=301]

    # ISAPI_Rewrite 3.0 版本
    [ISAPI_Rewrite]
    # 3600 = 1 hour
    CacheClockRate 3600
    RepeatLimit 32
    RewriteCond %{HTTP:Host} ^www.isapirewrite.com$
    RewriteRule (.*) http://www.farlee.info$1 [NC,R=301]

    3. 将页面301重定向到另外一个页面

    # ISAPI_Rewrite 2.x 版本
    [ISAPI_Rewrite]
    # 3600 = 1 hour
    CacheClockRate 3600
    RepeatLimit 32
    RewriteRule ^/oldpage.html$ http://farlee.info/newpage.html[I,O,RP,L]

    # ISAPI_Rewrite 3.0 版本
    [ISAPI_Rewrite]
    # 3600 = 1 hour
    CacheClockRate 3600
    RepeatLimit 32
    RewriteRule ^/oldpage.html$ http://farlee.info/newpage.html[NC,L,R=301,O]

    注意:ISAPI_Rewrite 1.3是现在国内应用得比较多的一个老版本,它可用于url 重写,但并不适合用来实现真正的301重定向功能。建议下载最新的ISAPI_Rewrite 3.0 版本。在 url转发和301转向(重定向跳转)的实现 这篇文章介绍了一段ISAPI_Rewrite 1.3 的重定向代码,这段代码在IIS下对域名重定向虽然能够顺利跳转,但是返回的仍然是302 HTTP header,而不是301 状态码。而且该段代码用于301重定向后,在blog中的其他页面都会跳转到首页。

    这样设置以后,你的windows iis 也能实现301重定向了,不管是Google 和百度搜索引擎都不会有因为带有www和不带www的域名而产生重复页面问题了。

    PHP 301 重定向代码

    301重定向也可以在php文件中通过加入php header来实现,代码如下:

    <?php
    header(“HTTP/1.1 301 Moved Permanently”);
    header(“Location: http://farlee.info/newpage.html”);
    exit();
    ?>

    ASP 301 重定向代码

    <%@ Language=VBScript %>
    <%
    Response.Status=”301 Moved Permanently”
    Response.AddHeader “Location”, http://farlee.info
    %>

    ASP.NET 301 重定向代码

    <script language=”c#” runat=”server”>
    private void Page_Load(object sender, System.EventArgs e)
    {
    Response.Status = “301 Moved Permanently”;
    Response.AddHeader(“Location”,http://farlee.info);
    }
    </script>

    ColdFusion 301 重定向代码

    <.cfheader statuscode=”301″ statustext=”Moved permanently”>
    <.cfheader name=”Location” value=”http://farlee.info/newpage.html”>

    CGI Perl下的301转向代码

    $q = new CGI;
    print $q->redirect(“http://farlee.info”);

    JSP下的301转向代码

    <%
    response.setStatus(301);
    response.setHeader( “Location”, “http://farlee.info” );
    response.setHeader( “Connection”, “close” );
    %>

    没想到这篇文章写了这么长,有那么多种301转向方法供我们选择,我们还要依赖域名url转发功能干什么呢?

  • 相关阅读:
    技术干货 | 闲鱼:一个优秀的 Push 平台,需要经历怎样的前世今生
    钉钉宜搭3.0发布!易连接、酷数据、更安全
    聚焦2021云栖大会,边缘云专场畅谈技术应用创新
    基层数字化治理困境如何破局?
    2021年阿里云双11上云狂欢节亿元加油包提前领攻略
    形式化验证工具TLA+:程序员视角的入门之道
    ArrayList源码浅析
    对云信SDK的研究
    写在当下
    Delphi 正则表达式起步
  • 原文地址:https://www.cnblogs.com/bluecobra/p/2659732.html
Copyright © 2011-2022 走看看