【问题标题】:www.sitename.com not rediecting to https://www.sitename.comwww.sitename.com 未重定向到 https://www.sitename.com
【发布时间】:2019-05-06 06:38:06
【问题描述】:

我需要如下 IIS mod_rewrite 的东西

  1. http://www.example.com 重定向到https://www.example.com

  2. example.comhttps://www.example.com

  3. www.example.comhttps://www.example.com

我正在使用的这个和 www.sitename.comsitename.com 不工作 - 出现 404 错误:

<rewrite>
  <rules>
    <clear />
    <rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^[^www]" />
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="sitename.com{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

【问题讨论】:

  • 请将您尝试过的所有内容添加到问题本身,而不是评论部分。另外解释一下究竟什么还不起作用

标签: windows iis mod-rewrite server


【解决方案1】:

这应该将http移动到https,只需将下面的代码添加到您的web.config

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true"> 
          <match url="(.*)" /> 
          <conditions> 
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions> 
          <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
        </rule> 
      </rules> 
    </rewrite> 
  </system.webServer>
</configuration>

对于 apache 用户,只需将此代码添加到 .htaccess 文件中

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

【讨论】:

  • 我在 IIS Windows 2012 中托管站点。.htacess 将在上面工作吗?
  • 我需要 Windows .webconfig 文件
  • 谢谢。我尝试使用您的 webconfig,但它不起作用。当我输入 www.servername.com 然后它没有重定向,它去 404 页面@David
  • @Matrix 试试这个新的,如果它不起作用,你应该考虑在帖子描述中添加你的web.config 代码
  • 我试过你的新的。问题是当我们在没有任何 http 或 https 的情况下进入时,它会重定向。但是在浏览器中输入 www.sitename.com 时它不起作用。或sitename.com 也不起作用。我正在使用 LetEncrypt SSL
猜你喜欢
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2015-04-05
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多