简体   繁体   中英

IIS rewrite rule not working in live environment

I have 4 servers in azure, 3 are load balanced and the 4th is for CMS purposes only.

SSL certificate has been added for the main website, but not for the sobdomain that the CMS is on.

I wrote a rule that should find any url that doesnt contain "backoffice" and match any other page to change it to https.

This works on regexr.com but for some reason doesnt work

<rewrite>
        <rules>
            <rule name="http to https" stopProcessing="true">
                <match url="(https?:\/\/(?!backoffice).*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
                <action type="Redirect" url="https://www.WEBSITENAME.com{R:1}" />
            </rule>
        </rules>
    </rewrite>

Url Rewriting 2.1 is installed on all 4 servers and i have created a load balance set in azure for https.

going to https manually works fine (along with loadbalancing).

Additional information:

I've tried many rules, including the existing answer. I can see things happening, like assets being brought in as https, but the page itself does not redirect.

There are 2 load balance sets, one for port 80 and the other for port 443. I don't know if this is corect, or could be a potential cause in the redirect not happening.

Your rule should be like that:

<rule name="http to https" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{REQUEST_URI}" pattern="/backoffice" negate="true" />
    </conditions>
    <action type="Redirect" url="https://www.WEBSITENAME.com{R:0}" />
</rule>

This rule will exclude requests with /backoffice path.

Also for issue of mixing content you need to fix your paths for css/js/images to relatives. Example:

<img src="/path/to/your/image.jpg"/>

Another way to fix mixed content is create outbound rule, which will change your output HTML (replace http: to https: ):

<rewrite>

  ...

  <outboundRules>
    <rule name="Rewrite external references to use HTTPS" preCondition="IsHTML">
      <match filterByTags="Script, Link, Img, CustomTags" customTags="HTML5Tags" pattern="^http://(.*)$" />
      <action type="Rewrite" value="https://{R:1}" />
    </rule>
    <preConditions>
      <preCondition name="IsHTML">
        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
      </preCondition>
    </preConditions>
    <customTags>
      <tags name="HTML5Tags">
        <tag name="Video" attribute="src" />
      </tags>
    </customTags>
  </outboundRules>
</rewrite>

Using the previous answer as a starting point, i made a few minor changes, to use HTTP_HOST rather than REQUEST_URI for the pattern negation and it works.

<system.webServer>
    <rewrite xdt:Transform="InsertIfMissing">
        <rules>
            <rule name="http to https" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                    <add input="{HTTP_HOST}" pattern="^backoffice\.WEBSITENAME\.com$" negate="true" />
                </conditions>
                <action type="Redirect" url="https://www.WEBSITENAME.com/{R:0}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

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