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

时间:2017-08-28 13:32:35

标签: redirect web-config subdomain

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

编辑:使用web.config文件

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

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

提前致谢, ~Squishling

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

3 个答案:

答案 0 :(得分:3)

是的,这是可能的。以下是IIS 10 + UrlRewrite 2.1。

的方法

例如,我们假设我有:

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

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

IIS管理器 - &gt;网站上下文菜单 - &gt; &#34;编辑绑定..&#34; 编辑接受的主机名以接受*.<yourDomain>。对于示例案例: enter image description here

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

现在网站应该回复http://good.localhost:81以及http://bad.localhost:81

第2步:安装Url重写模块

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

步骤3:配置重定向规则

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

测试

非好域名现在应该返回重定向响应:

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

答案 1 :(得分:0)

当然可以。 这是一个最小的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
  4. 测试

    squishling.co.uk foo.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服务器配置,除了具有单独配置的域之外,它们将拦截所有域。同样可以在其他Web服务器中完成

答案 2 :(得分:-1)

试试这个。参考https://stackoverflow.com/a/10662732/7877099

<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>