2017-09-15 40 views
0

我有一個RewriteRule,它將URL前綴「www」,假定它不存在。IIS RewriteRules

<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true"> 
    <match url="*" /> 
    <conditions> 
    <add input="{HTTP_HOST}" pattern="www.example.com" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" /> 
</rule> 

麻煩的是,我有,我不希望這種重寫規則應用於另一個入站域(www.example2.com)。我會認爲下面的工作會很好。

<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true"> 
    <match url="*" /> 
    <conditions logicalGrouping="MatchAll"> 
    <add input="{HTTP_HOST}" pattern="www.example.com" negate="true" /> 
    <add input="{HTTP_HOST}" pattern="www.example2.com" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" /> 
</rule> 

但它似乎被忽略。有什麼建議麼?謝謝!

回答

0

你可以用這條規則來做到這一點。它會增加WWW,只爲域example.com,將忽略所有其他領域:

<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true"> 
    <match url="*" /> 
    <conditions> 
    <add input="{HTTP_HOST}" pattern="^example.com$" /> 
    </conditions> 
    <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" /> 
</rule> 
0

維克多讓我最的方式存在。謝謝!

<rule name="Add www" stopProcessing="true"> 
     <match url=".*" /> 
     <conditions> 
     <add input="{HTTP_HOST}" pattern="^example.com$" /> 
     </conditions> 
     <action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" /> 
    </rule> 

patternSyntax必須是默認的正則表達式值。這意味着URL模式必須改變,並且回溯表達式是基於零的。