网站web.config>是否可以将不正确的子域重定向到某个页面?

Squ*_*ing 5 subdomain redirect web-config

是否可以将不正确的子域(不存在的子域)重定向到某个页面(自定义错误页面)?

编辑:使用web.config文件

例如,如果我键入http://foo.squishling.co.uk/,它将重定向到http://squishling.co.uk/errors/incorrect_subdomain.html/,如http://foo.squishling. co.uk/不是正确的子域名.

我没有看到任何代码执行此操作,所以它可能吗?
编辑:如果不可能,请说出来

在此先感谢,~Squishling

编辑:如果这是可能的,一个可能的方法是当服务器收到不正确的子域的请求,只是欺骗请求请求错误页面

Imr*_*vel 3

是的,这是可能的。以下是 IIS 10 + UrlRewrite 2.1 的使用方法。

例如,我们假设我有:

  • 有效的域/端口good.localhost:81
  • 我的自定义错误文件@/error.txt

步骤 1:设置站点绑定以接受子域请求

IIS 管理器 -> 网站上下文菜单 ->“编辑绑定..”编辑接受的主机名以接受*.<yourDomain>. 对于示例情况: 在此输入图像描述

详细步骤可以在文档页面“通配符主机标头支持”中找到。

现在网站应该同时响应http://good.localhost:81http://bad.localhost:81

Step2:安装Url重写模块

您可以从这里找到 URL 重写模块安装程序: https: //www.iis.net/downloads/microsoft/url-rewrite

Step3:配置重定向规则

虽然您可以使用 IIS MANager 的 GUI 来实现此目的,但您也可以将如下内容手写到您的 web.config 中:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
        <rules>
            <rule name="DirectBadSubdomainsRule" stopProcessing="true">
                <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^good\.localhost:81" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://good.localhost:81/error.txt" redirectType="Temporary" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

您可以使用正则表达式的强大功能来确定哪些子域有效,哪些子域无效。您还可以准确决定所需的重定向代码。上面的示例使用临时重定向 (HTTP307)。

这里有很多文档: https: //www.iis.net/downloads/microsoft/url-rewrite

测试

不良域名现在应该返回重定向响应:

  1. http://bad.localhost:81/ Correct.txt -> HTTP307
  2. http://good.localhost:81/error.txt -> HTTP200