返回 503 错误而不是 404

返回 503 错误而不是 404

我们将应用程序移至另一台服务器,并在旧服务器上的 IIS 中关闭了该站点。看起来仍有网络爬虫(Googlebot、百度等)在尝试旧 IP 地址,因此它们会收到 404 错误。我们希望它们收到 503 错误,以便它们稍后再试,这将使 DNS 有更多时间进行传播。有没有一种简单/直接的方法可以在 IIS6 中执行此操作?

答案1

今晚我要将几个网站从 IIS 6(Windows 2003 服务器)迁移到 IIS 7.5(Windows 2008 服务器),同时更改 IP 地址,并且还担心网络爬虫。一位朋友指出,如果我将旧服务器上的应用程序池脱机,而网站保持运行,则所有网站都会返回 503 错误代码,即使您尝试使用 IP 地址访问它们,直到 DNS 传播完成。似乎效果很好。

(我知道这是一篇迟来的帖子,但是我花了几个小时寻找这样的解决方案,所以我想把它添加到这里。)

答案2

您可以创建一个“catch-all”页面并添加以下 asp 代码:

context.context.Response.StatusCode =
(int)HttpStatusCode.ServiceUnavailable;

答案3

IIS 6这在和中有效IIS 7
这是针对我的特定商店 (ASPDotNetStorefront) 的,请根据需要进行修改。

网页配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.web>
        <customErrors defaultRedirect="/404handler.asp" mode="On" />
    </system.web>
</configuration>

404处理程序.asp

<%@ Language=VBScript %>
<%
Option Explicit

Sub Redirector()
    Dim strRequest
    Dim strRedirect
    Dim strServer
    Dim objRegExp
    Dim tokens

    strServer = "http://mynewurl.example.com"

    strRequest = Request.QueryString("aspxerrorpath")
    If StrComp(strRequest, "") = 0 Then
        strRequest = Request.QueryString
        strRequest = Mid( strRequest, Instr( Instr(1, strRequest, "://") + 3, strRequest, "/"))
    End If

    Set objRegExp = New RegExp
    objRegExp.IgnoreCase = True

    'Product Page
    objRegExp.Pattern = "^/p-[0-9]+-.*\.aspx$"
    If objRegExp.Test(strRequest) Then
        strRedirect = strServer + strRequest
        Call DoResponse(strRedirect)
    End If

    'Product Print Page
    objRegExp.Pattern = "^/print-([0-9]+)-(.*)\.aspx$"
    Set tokens = objRegExp.Execute(strRequest)
    If tokens.Count > 0 Then
        strRedirect = strServer + "/p-" + tokens(0) + "-" + tokens(1) + ".aspx?print=1"
        Call DoResponse(strRedirect)
    End If

    'Product Email Page
    objRegExp.Pattern = "^/email-([0-9]+)-(.*)\.aspx$"
    Set tokens = objRegExp.Execute(strRequest)
    If tokens.Count > 0 Then
        strRedirect = strServer + "/p-" + tokens(0) + "-" + tokens(1) + ".aspx"
        Call DoResponse(strRedirect)
    End If

    ' Signin Page
    objRegExp.Pattern = "^/signin.aspx(\?.*)?$"
    Set tokens = objRegExp.Execute(strRequest)
    If tokens.Count > 0 Then
        strRedirect = strServer + strRequest
        Call DoResponse(strRedirect)
    End If

    'Manufacturer Page
    objRegExp.Pattern = "^/m-[0-9]+-.*\.aspx(\?.*)?$"
    If objRegExp.Test(strRequest) Then
        strRedirect = strServer + Replace(strRequest, "CatID", "FilterID", 1, -1, vbTextCompare)
        Call DoResponse(strRedirect)
    End If

    'Category Page
    objRegExp.Pattern = "^/c-[0-9]+-.*\.aspx(\?.*)?$"
    If objRegExp.Test(strRequest) Then
        strRedirect = strServer + Replace(strRequest, "ManID", "FilterID", 1, -1, vbTextCompare)
        Call DoResponse(strRedirect)
    End If

    'Other ASPX Pages
    objRegExp.Pattern = "^/.*\.aspx(\?.*)?$"
    If objRegExp.Test(strRequest) Then
        Dim Message, a
        Message = "Original request was: " + strRequest + vbCrLf + vbCrLf + "Info:"
        Message = Message + vbCrLf + "  " + Request.ServerVariables("ALL_HTTP")
        Message = Message + vbCrLf + "  Client: " + Request.ServerVariables("REMOTE_ADDR")
        Call SendEmail("[email protected]", "[email protected]", "Missed ASPX 404 Error", Message)
        'Response.Write(Message)
    End If

    ' Ignore errors for non-aspx pages
End Sub

Sub SendEmail(seTo,seFrom,seSubject,seBody)
    on error resume next
    Dim EmailObj, iConf, Flds
    Set EmailObj = Server.CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields
    Flds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Flds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "localhost"
    Flds.Update
    EmailObj.Configuration = iConf
    EmailObj.From = Chr(34) & seFrom & Chr(34) & Chr(60) & seFrom & Chr(62)
    EmailObj.ReplyTo = Chr(34) & seFrom & Chr(34) & Chr(60) & seFrom & Chr(62)
    EmailObj.Subject = seSubject
    EmailObj.Fields.Update
    EmailObj.TextBody = seBody
    EmailObj.To = Chr(34) & seTo & Chr(34) & " <" & seTo & ">"
    EmailObj.Send
    Set EmailObj = nothing
    on error goto 0
End Sub

 Sub DoResponse(strRedirect)
    If StrComp(strRedirect, "") <> 0 Then
        Response.Status = "301 Moved Permanently"
        Call Response.AddHeader("Location", strRedirect)
        Call Response.Write("<html><head><title>Page moved</title></head>")
        Call Response.Write("<body><h1>Page Moved</h1>")
        Call Response.Write("This page may be found at <a href=""" & strRedirect & """>" & strRedirect & "</a>.")
        Call Response.Write("</body></html>")
        Call Response.End
    End If
End Sub

Call Redirector()
    Response.Status = "404 File not found"
%><?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
    <title>The Page Cannot be Found</title>
    <meta name="robots" content="noindex"></meta>
    <style>
    <!--
    BODY {
        FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif
    }
    H1 {
        COLOR: #cd5c5c
    }
    H2 {
        COLOR: #cd5c5c
    }
    TABLE {
        WIDTH: 540px
    }
    -->
    </style>
    </head>
    <body>
        <table>
            <tr>
                <td>
                <h1>The Page Cannot be Found</h1>
                <hr />
                <h2>Potential Causes:</h2>
                <ul>
                    <li>The requested file has been deleted.</li>
                    <li>The requested file has never existed.</li>
                    <li>The requested file is temporarily unavailable  due to maintenance, upgrades, or other similar reasons.</li>
                    <li>The requested file has moved, and we didn't automatically forward to the new page.</li>
                </ul>
                <hr />
                <h2>Please try the following:</h2>
                <ul>
                    <li>If you typed the page address into the web browser, make sure that you spelled it correctly.</li>
                    <li>Open the <a href="http://mynewurl.example.com/">home page</a> of this site.</li>
                    <li>Click the Back button to try another link.</li>
                </ul>
                <hr />
                <h2>HTTP 404 - File not found</h2>
                </td>
            </tr>
        </table>
    </body>
</html>

答案4

你知道什么会更容易吗?把这个放进去/robots.txt只要您不希望您的网站被搜索引擎机器人抓取:

User-Agent: *
Disallow: /

相关内容