Reputation: 419
The page should re-direct from http://example.com/... to https://www.example.com/... using 301 redirects.
The page should re-direct from http://www.example.com/... to https://www.example.com/... using a 301 redirect.
The page should re-direct from from https://example.com/... to https://www.example.com/... using a 301 redirect.
I have added following URL re-write in the web.config.
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:0}" />
</rule>
<rule name="Redirect example.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent"/>
</rule>
The above code doesn't work properly.
Upvotes: 0
Views: 549
Reputation: 7994
Another thing to consider is browser caching. Browsers cache 301 redirects which means if you set it up incorrectly at some point you will have the wrong redirect cached in your browser.
You need to clear your redirect cache in your browser to solve this problem.
Here are steps on clearing from Chrome taken from https://superuser.com/questions/304589/how-can-i-make-chrome-stop-caching-redirects
Chrome Menu Chrome Menu > Settings > Show advanced settings... > Privacy > Click Clear browsing data...
Whatever else you select, make sure "Cached images and files" is a checked option.
Then click Clear browsing data and you should be able to retest again.
If you've just followed the redirect, you only need to delete data from the past hour.
Alternatively, test and develop in incognito mode. There the cache is flushed after the browser is closed.
Upvotes: 0
Reputation: 113
Your redirect should be working, as I just tried them.
Try to reset the application pool of your website or kill the corresponding w3wp.exe process as the module URLrewrite is caching the settings and its possible that sometimes takes quite a while for new changes/rules to be working if you do not force reset that said cache.
Also make sure that both hosts www.example.com and example.com are with http and with https are on same IIS site that has the above rules.
Upvotes: 0
Reputation: 2077
Try these rules, you should try restarting the App Pool as well to ensure the new rules are picked up.
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" />
</rule>
<rule name="Redirect non-www to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>
Upvotes: 0