Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

asp.net - Web.config URL rewrite - force www prefix and https

I'm trying to enforce https and a www prefix. However my rule doesn't fully work. Here is my rule:

<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>

Please can somebody advise? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

These are the rewrite rules that I use for that exact purpose. I've also added a rule to make the URL all lowercase and a rule to remove the trailing slash should one be present. This makes working with Analytics easier since it treats page.aspx and page.aspx/ as different url's. That is why I use ignoreCase=true because then it does not matter if someone uses upper case somewhere since it will be handled later on by the ToLowerCase rule

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...