https://非www重定向到https:// www的问题

时间:2017-03-10 16:16:23

标签: http iis https binding

我的网站最近已从http://转移到https://

该站点使用在IIS7上托管的MVC构建,我知道我应该有4个绑定。

主要的一个:
        https://www.example.com(端口:443 - IP地址:*)

和另外3人全部到301重新指向上面。

    http://www.example.com - (port:80 - ip address: * -re-direct works)
    http://example.com - (port:80 - ip address: * -re-direct works)
    https://example.com - (port:443 - ip address: * -re-direct does not work)

尝试解决无法联系到底部失败的原因。无法找到服务器DNS地址。

以下是我用来将http重定向到https

的代码
            // The base only redirects GET, but we added HEAD as well. This avoids exceptions for bots crawling using HEAD.
            // The other requests will throw an exception to ensure the correct verbs are used. 
            // We fall back to the base method as the mvc exceptions are marked as internal. 

            if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)
                && !String.Equals(filterContext.HttpContext.Request.HttpMethod, "HEAD", StringComparison.OrdinalIgnoreCase))
            {
                base.HandleNonHttpsRequest(filterContext);
            }

            // Redirect to HTTPS version of page
            // We updated this to redirect using 301 (permanent) instead of 302 (temporary).
            string url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;

            filterContext.Result = new RedirectResult(url, true);

任何想法底部装订会出现什么问题?尝试在Google网站站长搜索控制台上设置第4个属性,但由于DNS失败,显然无法进行。

非常感谢任何帮助。干杯

2 个答案:

答案 0 :(得分:0)

最好在网络服务器级别而不是应用程序代码中执行此类操作。 对于IIS,您需要通过添加重写规则来更改文件 Web.config 。 我无法测试它,但你应该添加类似的东西:

<rewrite>
  <rules>
    <rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">              
      <match url="*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="example.com" />
      </conditions>
      <action type="Redirect" url="http://www.example.com/{R:0}" />
    </rule>

    <rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions>
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>

答案 1 :(得分:0)

问题出在域名提供商的DNS上。