简体   繁体   中英

Redirect a site to SSL using IIS/rewrite module

Im trying to redirect all urls of a site to SSL using IIS/rewrite module.

I have this rule which works fine for all urls except ones that contain a virtual directory/application called 'blog'

 <rule name="Force WWW and SSL" 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>     

So redirecting www.example.com/blog/post/test to https doesn't work, but www.example.com/pages/demo does.

And for some reason www.example.com/blog redirects but www.example.com/blog/ does not.

If someone could point me in the right direction please. I hate this rewrite/regex stuff

If you want to force HTTP to HTTPS for a particular site on all URLs and hate Regex then

  • Simply create a URL Rewrite rule on site.
  • Use wildcard type as they are little easy to understand.

This should work for HTTP to https

<rewrite>
<rules>
    <rule name="HTTP to HTTPS" patternSyntax="Wildcard" stopProcessing="true">
        <match url="*" />
        <conditions>
        <add input="{HTTPS}" pattern="off" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>
</rules>

Normally when I am playing with urlrewrite / redirection I use redirect type "temporary" so that browser doesn't cache the HTTP to HTTPS rule. Once I finish my testing I change redirect type to permanent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM