2016-05-18 17 views
0

我在堆棧溢出中經歷了許多類似的問題,但是我一直未能找到這有助於我將所有這三個重定向規則一起工作。在IIS中,我如何將所有URL轉發到非www,https和沒有文件擴展名的文件,而沒有太多重定向

我想我所有的URL轉發到這​​個格式:

https://example.com/page 

我需要https的,我不想讓WWW,我不希望HTML或ASPX擴展是可見。這是我到目前爲止。 Chrome說我有太多的重定向,我如何整合這些規則?

<rewrite> 
     <rules> 
      <clear /> 

      <rule name="RewriteHTML"> 
       <match url="(.*)" /> 
       <conditions logicalGrouping="MatchAll"> 
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
       </conditions> 
       <action type="Rewrite" url="{R:1}.html" /> 
      </rule> 

      <rule name="Redirect www to non-www" enabled="true" stopProcessing="true"> 
       <match url="(.*)\.html$" /> 
       <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
        <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" /> 
       </conditions> 
       <action type="Redirect" url="http://example.com/{R:1}" redirectType="Permanent"/> 
      </rule> 

      <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true"> 
       <match url="(.*)\.html$" /> 
       <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
        <add input="{HTTPS}" pattern="^OFF$" /> 
       </conditions> 
       <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/> 
      </rule> 

      <rule name="RemoveExtension" stopProcessing="true"> 
       <match url="(.*)\.html$" /> 
       <action type="Redirect" url="{R:1}" redirectType="Permanent" /> 
      </rule> 

     </rules> 
    </rewrite> 

回答

0

我終於想出花樣與規則的訂單玩耍後,和stopProcessing命令等,沒有更多的重定向循環!我真的知道我在問什麼,還有更多。

  1. 移除從根域的index.html
  2. 重定向WWW到非www
  3. 移除尾隨斜線
  4. 使得URL總是小寫
  5. 重定向HTTP://到https://
  6. 使URL無效.html擴展名
  7. 重定向到不帶.html擴展名的URL

我的代碼:

<rules>   
    <!-- index.html Redirect -->  
    <rule name="index.html Redirect" enabled="true"> 
     <match url="^(.*\/)*index\.html$" /> 
     <conditions logicalGrouping="MatchAll"> 
      <add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" /> 
     </conditions> 
     <action type="Redirect" url="{R:1}" redirectType="Permanent" /> 
    </rule> 

    <!-- Redirect WWW to non-WWW --> 
    <rule name="Redirect WWW to non-WWW" enabled="true"> 
     <match url="(.*)" /> 
     <conditions> 
      <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" /> 
     </conditions> 
     <action type="Redirect" url="http://example.com/{R:1}" /> 
    </rule> 

    <!-- Remove Trailing Slash --> 
    <rule name="Remove Trailing Slash" enabled="true"> 
     <match url="(.*)/$" /> 
     <conditions> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
     </conditions> 
     <action type="Redirect" url="{R:1}" /> 
    </rule> 

    <!-- Make URL Lower Case --> 
    <rule name="Make URL Lower Case" enabled="true"> 
     <match url="[A-Z]" ignoreCase="false" /> 
     <action type="Redirect" url="{ToLower:{URL}}" /> 
    </rule> 

    <!-- Redirect To HTTPS --> 
    <rule name="Redirect To HTTPS" enabled="true"> 
     <match url="(.*)" /> 
     <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
      <add input="{HTTPS}" pattern="^OFF$" /> 
     </conditions> 
     <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> 
    </rule> 

    <!-- Redirect To URL With No Extension --> 
    <rule name="Redirect To URL With No Extension" enabled="true"> 
     <match url="(.*)\.(html)" /> 
     <action type="Redirect" url="{R:1}" /> 
    </rule>    

    <!-- Rewrite URL For No Extension --> 
    <rule name="Rewrite URL For No Extension" enabled="true" stopProcessing="false"> 
     <match url="(.*)" /> 
     <conditions logicalGrouping="MatchAll"> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
     </conditions> 
     <action type="Rewrite" url="{R:0}.html" /> 
    </rule> 
</rules> 

這裏的規則是什麼樣子的IIS URL重寫模塊:

URL Rewrite Rule Details

相關問題