Mfusiki Mfusiki - 3 months ago 38
ASP.NET (C#) Question

Intelligencia.UrlRewriter check domain to redirect to https

I'm having a problem trying to force https on my website while excluding locahost
I'm currently using Intelligencia.UrlRewriter to rewrite any urls and I have the following rule set up:

<if header="HTTPS" match="^OFF$">
<redirect url="(.*)" to="https://sub.mydomain.com$1" />
</if>


This redirects fine, but I would like to make sure that my localhost debugging is not affected. This obviously runs under a different URL. So I tried:

<if header="HTTPS" match="^OFF$">
<redirect url="(.*)mydomain" to="https://sub.mydomain.com$1" />
</if>


This however does not work. I believe the reason for that might be that it only evaluates anything after the .com.
I have tried searching for documentation with no luck.

How can I always force https connection except on my localhost?

Answer

I managed to find a solution to my problem:

<unless header="HTTP_HOST" match="localhost">
  <if header="HTTPS" match="^OFF$">
    <redirect url="(.*)" to="https://sub.mydomain.com$1" />
  </if>
</unless>

With the addition of the <unless header="HTTP_HOST" match="localhost"> tag I can exclude certain hosts and still allow other hosts to be redirected to https. In other words:

This will be forced:


http://sub.mydomain.com/default.html

To (note the s in https):

https://sub.mydomain.com/defualt.html


But this will not be changed:

http://localhost/default.html


Comments