Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 609
Hazonko Avatar asked Sep 05 '17 15:09

Hazonko


People also ask

How do I know if URL Rewrite is working?

Checking if the URL Rewrite module is installed To see if the URL Rewrite module is installed, open IIS Manager and look in the IIS group - if the module is installed, an icon named URL Rewrite will be present. The screenshot below shows an example of a server when the module is installed.

Where are IIS rewrite rules stored?

When done on the server level it is saved in the ApplicationHost. config file. You can also define it on the folder level, it that case it is saved in a web. config file inside that folder.


1 Answers

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>
like image 180
Victor Leontyev Avatar answered Sep 18 '22 13:09

Victor Leontyev