【问题标题】:Website web.config > Is it possible to redirect an incorrect subdomain to a certain page?网站 web.config > 是否可以将不正确的子域重定向到某个页面?
【发布时间】:2018-02-05 19:14:41
【问题描述】:

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

编辑:使用 web.config 文件

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

我还没有看到任何代码可以做到这一点,那么有可能吗?
编辑:如果不可能,请说出来

提前致谢, ~ 压扁

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

【问题讨论】:

    标签: redirect web-config subdomain


    【解决方案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>
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    【解决方案2】:

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

    例如,假设我有:

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

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

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

    详细步骤可参考文档页面"Wildcard Host Header Support"。

    现在网站应该同时回复http://good.localhost:81 和http://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>
    

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

    【讨论】:

      【解决方案3】:

      当然可以。 这是执行此操作的最小 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.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-config 表明 OP 对 IIS 的解决方案感兴趣,而不是 nginx。
      猜你喜欢
      • 2021-09-27
      • 1970-01-01
      • 2021-03-02
      • 2020-08-15
      • 2021-03-29
      • 2016-04-20
      • 2017-04-12
      • 2015-02-16
      • 2014-01-14
      相关资源
      最近更新 更多