Rewrite URL to remove www and redirect to https using web-config (C # .net)

I have the following code in my web configuration in order to be able to redirect both URLs with the prefix "www" and non-SSL requests to https://mydomain.com, because the SSL certificate is registered in the domain without www

<rewrite> <rules> <rule name="Remove WWW prefix and redirect to https" > <match url="(.*)" ignoreCase="true" /> <conditions logicalGrouping="MatchAny"> <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" ignoreCase="true" /> <add input="{HTTPS}" pattern="off" ignoreCase="true" /> </conditions> <action type="Redirect" redirectType="Permanent" url="https://mydomain.com/{R:1}" /> </rule> </rules> </rewrite> 

This is the result:

1) http://mydomain.com/something → https://mydomain.com/something (correct)

2) http://www.mydomain.com/something → https://mydomain.com/something (correct)

3) https://www.mydomain.com/something → Shows a certificate error (there is a problem with this website security certificate.)

If you select "Continue on this site (not recommended)." URL certificate rewritten on page with certificate error (https://mydomain.com/something)

How can I make sure the certificate error is not displayed?

thanks

+6
source share
4 answers

therefore, we use it in our projects, and it works.

Let me know what helps:

 <rewrite> <rules> <rule name="Redirect to https"> <match url="(.*)"/> <conditions> <add input="{HTTPS}" pattern="Off"/> <add input="{REQUEST_METHOD}" pattern="^get$|^head$" /> <add input="{HTTP_HOST}" pattern="localhost" negate="true"/> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/> </rule> </rules> </rewrite> 

It also ignores the request when accessing the site on your local machine.

+2
source

I'm not sure about this because I don't have too much experience with rewriting url, but trying to help won't hurt.
You can try this

 <conditions> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://mydomain.com/{R:1}" redirectType="Permanent" /> 

I searched a lot in googled and found this, but it might not do what you wanted.

0
source

One way to solve it is to register two separate rules:

  • Delete www.
  • The power of HTTPS.

     <rule name="Remove www" stopProcessing="true"> <match url="(.*)" negate="false"></match> <conditions> <add input="{HTTP_HOST}" pattern="^www\.(.*)$" /> </conditions> <action type="Redirect" url="https://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" /> </rule> <rule name="Force HTTPS" enabled="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> 
0
source

This problem cannot be solved with the help of rewrite rules: the problem is that the certificate is checked at the moment of establishing a connection with the server. Since your server does not have a valid certificate for the www. option www. , the certificate is invalid and the browser will notify its user.

Only after the user agrees to continue, the request is sent to the server, and the rewrite rules begin.

0
source

All Articles