2016-04-19 4 views
2

私は私のASP.NET MVC 5プロジェクトのweb.configで次の書き換えルールがあります。「非WWW WWWへ」と「https」のASP.NET MVCをlocalhostのweb.configファイル内のルールを書き換えなく

<rule name="Redirect example.com to www.example.com and enforce https" enabled="true" stopProcessing="true"> 
    <match url="(.*)" /> 
    <conditions logicalGrouping="MatchAny"> 
     <add input="{HTTP_HOST}" pattern="^[^www]" /> 
     <add input="{HTTPS}" pattern="off" /> 
    </conditions> 
    <action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" /> 
</rule> 

ルールはwww以外のものをwwwとhttpをhttpsにリダイレクトします(http://example.com/heyhttps://www.example.com/heyにリダイレクトされます)。正常に動作します。しかし、それはまたlocalhostで動作し、私はそれを回避することができないようです - 私は否定規則と正規表現を試してみました|が含まれていますが、正しい組み合わせを見つけることができないようです。私はこれに間違った方法で近づいていますか?

答えて

3

conditionsブロックでは、属性negate="true"を使用できます。この属性が設定され、条件が一致すると、書き換えルールは適用されません。

IIS.netからnegate属性の説明:

パターン 要素のネゲート属性を使用して否定することができます。この属性を使用すると、現在のURLが指定されたパターンと一致しない場合にのみ、ルールアクションが実行されます( )。

MatchAnyを使用しているため、少なくとも1つの条件が満たされているため、追加属性を追加すると一致しません。私は、それぞれが1つだけの場合に責任があるlogicalGrouping="MatchAll"、で2つの固有の書き換えルールを使用することをお勧めします

<rule name="enforce https" enabled="true" stopProcessing="true"> 
    <match url="(.*)" /> 
    <conditions logicalGrouping="MatchAll"> 
     <add input="{HTTPS}" pattern="off" /> 
     <add input="{HTTP_HOST}" matchType="Pattern" 
       pattern="^localhost(:\d+)?$" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" 
      appendQueryString="true" redirectType="Permanent" /> 
</rule> 
<rule name="Redirect example.com to www.example.com" 
     enabled="true" stopProcessing="true"> 
    <match url="(.*)" /> 
    <conditions logicalGrouping="MatchAll"> 
     <add input="{HTTP_HOST}" pattern="^[^www]" /> 
     <add input="{HTTP_HOST}" matchType="Pattern" 
       pattern="^localhost(:\d+)?$" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="https://www.example.com/{R:1}" 
     appendQueryString="true" redirectType="Permanent" /> 
</rule>  
+0

おかげで、私はライブサイト上のChromeでこれを取得しています:「www.example.comあなたはあまりにも多くのリダイレクトFirefoxのように – globetrotter

+0

@globetrotter私はそれが 'MatchAny'の問題だと思っています - ' MatchAll'で更新された答えが – dotnetom

+0

に役立つかどうか確認してください。ライブサイトでは動作しますが、 'localhostに' https'を追加しようとします。 ' – globetrotter

関連する問題