2012-10-23 67 views
4

我遇到问题。在IIS我有一个网站有两个端口80443(https)。我想将用户的所有http请求重定向到https。我也加了Rewrite rule to https,但是当我在浏览器http://localhost/site中输入时,它给了我相同的页面。我需要将用户重定向到httpS://localhost/siteURL从http重写到https不起作用,

也许这是因为我的本地配置?

而我禁用Require SSLIIS

The rule is: 
<rewrite> 
    <rules> 
    <rule name="HTTPS Redirect"> 
     <match url="(.*)" ignoreCase="false" /> 
     <conditions> 
     <add input="{HTTPS}" pattern="off" ignoreCase="false" /> 
     </conditions> 
     <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" /> 
    </rule> 
    </rules> 
</rewrite> 

谢谢。

+0

规则在哪里# – ChrisBint

+0

已编辑。谢谢。 –

回答

14

下面是我们使用在生产的确切规则IIS 7的网站从HTTP请求都重定向到HTTPS

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

之间存在着我们使用已发布什么以及一些细微的差别。另外,既然你在本地主机上运行,​​你会不会在visual studio中使用内置的web服务器?我不认为它会处理IIS重写规则。

+0

是的,你是对的。我已经解决了这个问题。问题在于不正确的网址。谢谢。 –

+0

@NickitaFabregas真棒 - 很高兴你明白了。你应该自己张贴和标记答案,或者将上面的解答之一标记为答案,这样对于那些未来可能会遇到的问题,它不会成为一个开放/未答复的问题! – Tommy

+0

这个配置文件应该放在哪个文件中? –

0

您可以将其添加到您的网页。强制重定向。

if (!Request.IsSecureConnection) 
{ 
Uri uri = new Uri(Request.Url, Request.RawUrl); 
Response.Redirect(string.Format("https://{0}{1}{2}", uri.Host.StartsWith("www.x.com") ? uri.Host : "www.x.com", uri.AbsolutePath, uri.Query)); 
} 

我刚才看到你的MVC

标签这个属性添加到你的行动。或控制器。

[RequireHttps] 
+0

已经解决。谢谢。 –

3

我知道这可能不是你的问题,但我也有类似的崩溃,这是由不同的问题的原因。

我已经启用了要求SSL并且导致站点不断返回403.所以要使用此方法,您必须禁用SSL设置 - >需要SSL。

希望这可以帮助别人。

0

此外,如果您有多个规则,订单可能很重要。确保在其他规则或重定向可能无法触发之前添加重定向规则。

以下是使用需要规则顺序的SPA的示例。

<rules> 

     // Adding this as last rule will cause it to not redirect 
     <rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true"> 
     <match url="(.*)" /> 
     <conditions logicalGrouping="MatchAny"> 
      <add input="{SERVER_PORT_SECURE}" pattern="^0$" /> 
     </conditions> 
     <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" /> 
     </rule> 


     <rule name="static dist files" stopProcessing="true"> 
     <match url="^(.+)" /> 
     <conditions> 
      <add input="{APPL_PHYSICAL_PATH}app\{R:1}" matchType="IsFile" /> 
     </conditions> 
     <action type="Rewrite" url="/app/{R:1}" /> 
     </rule> 
     <rule name="index.html as document root" stopProcessing="true"> 
     <match url="^$" /> 
     <action type="Rewrite" url="/app/" /> 
     </rule> 
     <rule name="SPA Routes" stopProcessing="true"> 
     <match url=".*|.*/.*$" /> 
     <conditions logicalGrouping="MatchAll"> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
      <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" /> 
     </conditions> 
     <action type="Rewrite" url="/app/" /> 
     </rule> 
    </rules>