Web.config URL重写 - 强制www前缀和https

时间:2017-02-22 20:21:33

标签: asp.net url-rewriting web-config

我正在尝试强制执行https和www前缀。但是我的规则并不完全有效。这是我的规则:

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

有人可以建议吗?感谢。

2 个答案:

答案 0 :(得分:7)

这些是我用于此目的的重写规则。我还添加了一个规则,使URL全部小写,并添加一个规则来删除尾随斜杠,如果存在一个。这使得使用Google Analytics更容易,因为它将page.aspx和page.aspx /视为不同的url。这就是我使用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>

答案 1 :(得分:1)

以下是此类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