Web.config URL rewrite - force www prefix and https

隐身守侯 提交于 2019-12-21 22:50:20

问题


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>
  • It works for redirecting http to https.
  • it works if I go to https://mydomain.co.uk (redirects to https://www.mydomain.co.uk)
  • however it DOES NOT work if I go to https://mydomain.co.uk/blah/whatever

Please can somebody advise? Thanks.


回答1:


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>



回答2:


Here is an example of such web.config -- it will force HTTPS for ALL resources (using 301 Permanent Redirect):

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

Source: https://stackoverflow.com/a/9823208/5740382

For more Details: https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference



来源:https://stackoverflow.com/questions/42401506/web-config-url-rewrite-force-www-prefix-and-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!