Rewrite Rule https://www to https:// IIS7
I’ve received my ssl certificate on a shared hosting provided and installed it successfully. The certificate is prepared for mysite.com, not for *.mysite.com. I want the site to be accessible like https : // mysite.com – no www, no http (only secure connections). I've found similar questions here, but not the right one. I'm using IIS 7, .net environment. The following rules were added my web.config:
<rule name="Remove WWW prefix">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mysite\.com" />
</conditions>
<action type="Redirect" url="https://mysite.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTPS">
<match url="(.*)" />
<conditions>
<add input="{SERVER_PORT}" pattern="443" negate="true" />
</conditions>
&l开发者_运维问答t;action type="Redirect" url="https:// mysite.com/{R:1}" />
Everything is working fine, but the url https://www.mysite.com is not redirected to https://mysite.com. I get the error that the certificate is for mysite.com (which was intended :), the message comes from firefox) with the advice to add exception.
My question is what is the appropriate rule for this url?
Thank you in advance for your time.
EDIT: Sorry, I think I misread your post! I have given you a rule to go from http to https automatically. I'll leave it here in case it helps someone else who (like me) also can't read and you could tweak it to make it fit your case.
Read this post by Omar Al Zabir: http://omaralzabir.com/redirecting-traffic-from-http-to-https-with-zero-coding-in-iis/
And then see my (Tom Chantler) comment to his post.
Those 2 rules worked for me.
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://mysite.com{PATH_INFO}" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
</rule>
Additional reading:
http://www.jppinto.com/2010/03/automatically-redirect-http-requests-to-https-on-iis7-using-url-rewrite-2-0/
http://keyvan.io/remove-www-prefix-from-urls-with-url-rewrite-module-for-iis-7-0
Unfortunately I don't think you can do this. You need a valid SSL certificate on https://www.mysite.com to get redirected to https://mysite.com without warning the user about an invalid certificate. IIS will attempt to negotiate the SSL connection before URL Rewrite has a chance to act on it.
You could possibly do this with a DNS entry. You could add a CNAME entry that looks like this:
www.mysite.com. CNAME mysite.com.
精彩评论