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

Website web.config > Is it possible to redirect an incorrect subdomain to a certain page?

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

编辑:使用 web.config 文件

例如,如果我键入 http://foo.squishling.co.uk/ it would redirect to something like http://squishling.co.uk/errors/incorrect_subdomain.html/, as http://foo.squishling.co.uk/ 是不正确的子域。

我还没有看到任何执行此操作的代码,这可能吗?
编辑:如果不可能,请说明

提前致谢, ~ 压扁

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

当然可以。 这是执行此操作的最小 nginx 配置:

server {
    listen 80 default_server;
    server_name _;
    location / {
        rewrite ^.*$ http://squishling.co.uk/errors/incorrect_subdomain.html redirect;
    }
}

server {
    listen 80;
    server_name squishling.co.uk;

    location / {
        echo some page;
    }

    location /errors/incorrect_subdomain.html {
        echo incorrect subdomain error;
    }
}

这是运行的方法:

  1. mkdir conf.d
  2. 将上面的配置文件放入conf.d/main.conf
  3. docker run -p80:80 -v ~/work/test/subreq/conf.d:/etc/nginx/conf.d openresty/openresty:alpine

测试

squishling.co.ukfoo.squishling.co.uk 在我的 /etc/hosts 中被覆盖,所以他们直接到我的本地主机

curl -D - http://squishling.co.uk
HTTP/1.1 200 OK
Server: openresty/1.13.6.1
Date: Tue, 06 Feb 2018 18:49:25 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive

some page

正在尝试错误的域

curl -D - http://foo.squishling.co.uk
HTTP/1.1 302 Moved Temporarily
Server: openresty/1.13.6.1
Date: Tue, 06 Feb 2018 18:49:34 GMT
Content-Type: text/html
Content-Length: 167
Connection: keep-alive
Location: http://squishling.co.uk/errors/incorrect_subdomain.html

<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>openresty/1.13.6.1</center>
</body>
</html>

或重定向后相同:

curl -L -D - http://foo.squishling.co.uk
HTTP/1.1 302 Moved Temporarily
Server: openresty/1.13.6.1
Date: Tue, 06 Feb 2018 18:50:03 GMT
Content-Type: text/html
Content-Length: 167
Connection: keep-alive
Location: http://squishling.co.uk/errors/incorrect_subdomain.html

HTTP/1.1 200 OK
Server: openresty/1.13.6.1
Date: Tue, 06 Feb 2018 18:50:03 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive

incorrect subdomain error

因此,主要思想是定义 default 服务器配置,该配置将拦截除具有单独配置的域之外的所有域。同样的事情也可以在其他网络服务器上完成

是的,这是可能的。这是使用 IIS 10 + UrlRewrite 2.1.

的方法

为了举例的目的,我们假设我有:

  • 有效domain/portgood.localhost:81
  • 我的自定义错误文件@/error.txt

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

IIS 管理器 -> 网站上下文菜单 ->“编辑绑定..” 编辑接受的主机名以接受 *.<yourDomain>。对于示例案例:

详细步骤可在文档页面 "Wildcard Host Header Support" 中找到。

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

第二步:安装Url重写模块

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

第三步:配置重定向规则

虽然您可以为此使用 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>

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

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

测试

非好域现在应该return重定向响应:

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

试试这个。参考资料来自

<configuration>
    <system.webServer>
        <httpRedirect enabled="true" destination="http://squishling.co.uk/errors/" exactDestination="true" httpResponseStatus="Permanent" />
    </system.webServer>
    <location path="incorrect_subdomain.html">
        <system.webServer>
            <httpRedirect enabled="false" />
        </system.webServer>
    </location>
</configuration>