2017-02-22 57 views
0

我試圖強制https和www前綴。然而,我的規則並沒有完全奏效。這是我的原則:Web.config URL重寫 - 強制www前綴和https

<rewrite> 
    <rules> 
    <clear />    
    <rule name="Force https" stopProcessing="true"> 
     <match url="(.*)" /> 
     <conditions> 
      <add input="{HTTPS}" pattern="off" ignoreCase="true" /> 
     </conditions> 
     <action type="Redirect" url="https://www.mydomain.co.uk/{R:1}" redirectType="Permanent" /> 
    </rule> 
    <rule name="Force www" stopProcessing="true"> 
     <match url="(.*)" /> 
     <conditions> 
      <add input="{HTTP_HOST}" pattern="localhost" negate="true" /> 
      <add input="{HTTP_HOST}" pattern="www.mydomain.co.uk" negate="true" /> 
     </conditions> 
     <action type="Redirect" url="https://www.mydomain.co.uk/{R:1}" redirectType="Permanent" /> 
    </rule>   
    </rules> 
</rewrite> 

請可有人建議它不工作?謝謝。

回答

3

這裏是這樣的web.config中的一個例子 - 這將迫使HTTPS的所有資源(使用301永久重定向):

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <system.webServer> 
     <rewrite> 
      <rules> 
       <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://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" /> 
       </rule> 
      </rules> 
    </rewrite> 


    <rewrite> 
     <rules> 
      <rule name="Redirects to www.example.com" patternSyntax="ECMAScript" stopProcessing="true"> 
       <match url=".*" /> 
        <conditions logicalGrouping="MatchAny"> 
         <add input="{HTTP_HOST}" pattern="^example.com$" /> 
        </conditions> 
        <action type="Redirect" url="https://www.example.com/{R:0}" /> 
       </rule> 
      </rules> 
     </rewrite>   
</system.webServer> 

來源:https://stackoverflow.com/a/9823208/5740382

欲瞭解更多詳情:https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

1

這些是我用於確切目的的重寫規則。我還添加了一個規則,使URL全部小寫,並且規則可以刪除尾部的斜線。這樣可以更輕鬆地使用Google Analytics,因爲它將page.aspx和page.aspx /視爲不同的網址。這就是爲什麼我使用ignoreCase=true因爲這時如果有人的地方使用大寫字母,因爲它會由ToLowerCase規則

<rule name="ForceWWW" stopProcessing="true"> 
    <match url=".*" ignoreCase="true" /> 
    <conditions> 
    <add input="{HTTP_HOST}" pattern="^yoursite.com" /> 
    </conditions> 
    <action type="Redirect" url="https://www.yoursite.com/{R:0}" redirectType="Permanent" /> 
</rule> 

<rule name="HTTPtoHTTPS" stopProcessing="true"> 
    <match url="(.*)" ignoreCase="false" /> 
    <conditions> 
    <add input="{HTTPS}" pattern="off" /> 
    </conditions> 
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> 
</rule> 

<rule name="RemoveTrailingSlash"> 
    <match url="(.*)/$" /> 
    <conditions> 
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
    </conditions> 
    <action type="Redirect" redirectType="Permanent" url="{R:1}" /> 
</rule> 

<rule name="ToLowerCase"> 
    <match url=".*[A-Z].*" ignoreCase="false" /> 
    <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" /> 
    <conditions> 
    <add input="{URL}" pattern="WebResource.axd" negate="true" /> 
    <add input="{URL}" pattern="ScriptResource.axd" negate="true" /> 
    </conditions> 
</rule> 
+0

由於後來的處理也沒關係。爲什麼使用'stopProcessing =「true」'?這不妨礙以下規則的處理嗎? –

+0

是的。 「_A規則可能會打開StopProcessing標誌,當此標誌打開時,這意味着不會再處理後續規則,並且由此規則生成的URL將傳遞到IIS請求管道(如果規則匹配)。默認情況下,此標誌已關閉._「請參閱https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference – VDWWD

+0

但這意味着如果請求缺少WWW前綴和HTTPS協議。 WWW前綴將被添加,但HTTPS不會因爲沒有更多的規則被處理。 –