2012-10-23 4 views
4

문제가 있습니다. IIS에는 두 개의 포트 80443(https)이있는 웹 사이트가 있습니다. 사용자의 모든 http 요청을 https으로 리디렉션하고 싶습니다. 나는 또한 Rewrite rule to https을 추가했지만, 브라우저에 입력하면 http://localhost/site 그것은 나에게 같은 페이지를 준다. 사용자를 httpS://localhost/site으로 리디렉션해야합니다.http에서 https 로의 URL 재 작성이 작동하지 않습니다.

내 로컬 구성 때문일 수 있습니다.

IIS에서 Require SSL을 사용 중지합니다.

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

아래는 우리가 생산에서 사용하는 정확한 규칙입니다 HTTP에서 HTTPS로 모든 요청을 리디렉션 (7) 사이트를 IIS

<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에서 기본 제공 웹 서버를 사용하지 않습니까? IIS 재 작성 규칙을 처리 할 것이라고는 생각하지 않습니다.

+0

네 말이 맞아. 이미이 문제를 해결했습니다. 문제는 올바르지 않은 URL로 인한 것입니다. 고맙습니다. –

+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> 
관련 문제