1

I have been over the web hunting for solutions for this, but I can get only 1 and 2 to work.

  1. http://www.example.com to https://www.example.com
  2. www.example.com to https://www.example.com
  3. http://example.com to https://www.example.com
  4. https://example.com to https://www.example.com
  5. https://example.com to https://www.example.com
  6. example.com/page.aspx to https://www.example.com/page.aspx

Requirements:

  • All URLs to be HTTPS
  • All URLs without WWW in them to show WWW
  • All other redirects to work, query strings and params carried over etc.

So the main issue is that if no protocol is specified (http or https) then if I enter example.com/blah then it gets converted to www.example.com/blah but put a protocol (http or https) in front and it breaks the link.

As I don't currently have a rule to move non-WWW URLs to WWW, then I am not sure what is doing it, if I have to do it, how I can get it to work with HTTP to HTTPS.

  • I have tried putting the rules for HTTP to go to HTTPS together with a rule for non-WWW to WWW but it didn't work (first example you will see - bottom example is what I am using at the moment).

  • I have tried using {HTTP_HOST} instead of writing out my site's URL in the redirect part of the rule.

  • I have tried splitting the rules into 2, one for HTTP to HTTPS, and one for no-WWW to WWW.

However nothing seems to work.

At the moment I am just using my hosts file and practising, changing the rules in the web.config file for the site on Win 2012 box.

I have other rules as well e.g for www.example.com/plugins to go to the page (rewrite) underneath .aspx, but these don't work either if there is no www in the URL.

So it seems the redirection of non-WWW URLs to WWW is the issue and I don't know the best way to combine it with the HTTP to HTTPS rules.

I was trying a combo of the two rules which covers the HTTPS/WWW and works apart from no 4/5 (non-WWW to WWW) this is that rule.

<rule name="Redirect to HTTPS" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
                <add input="{HTTPS}" pattern="^OFF$" />
                <add input="{HTTP_HOST}" pattern="^[^www]" />
                </conditions>
                <action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
             </rule>

The actual switching of logicalGrouping="MatchAny" to "MatchAll" doesn't seem to make a difference by the way.

As long as someone doesn't type the URL as http://example.com/plugins then it works fine and is redirected to https://www.example.com/plugins.

Not that I know many people who actually type the protocol in when entering links now (// works as well) but it's obviously the old search engines, and site embedded links I need to handle for duplicate content.

Can anyone think of a reason why this isn't working OR what I should try?

I have bindings set up for both port 80/443 for WWW and without.

I never find in answers to problems like these that people talk much about the IIS settings such as bindings and ports but I think they should as web.config is tied into the URL Redirect application and bindings are obviously required for your addresses.

I just find it weird that the example rule I put up earlier works just as well as this one which only mentions HTTP to HTTPS.

This is what I am currently using. Maybe IIS does something with www?

<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}/{R:1}" redirectType="SeeOther" />
             </rule>

I even changed the order of the bindings so that non-WWW bindings went before the WWW bindings thinking that might have something to do with it.

I control my DNS and my A record is set like

DNSEntry - WWW
Type - A
Destination - MY IP

So I am a bit confused to what is actually forcing non-WWW URLs to go to WWW URLs. As the command I am currently using in web.config only mentions HTTPS.

I am thinking maybe there is something in IIS (I am not an IIS 8 expert) that has set something to do it or the setting of one of the values should have been a domain without WWW in it.

Any help would be much appreciated. I doubt there are many URLs about pointing to my site without the WWW in it anyway but it would be good to know for SEO that I could force them all to one place so I don't get caught on duplicate content.

unor
  • 92,415
  • 26
  • 211
  • 360
MonkeyMagix
  • 677
  • 2
  • 10
  • 30

1 Answers1

0

Try below code:

    <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="Permanent" />
    </rule>

How to force HTTPS using a web.config file

IIS 7.5 URL Redirect for specific patterns

Override an IIS rewrite rule for child site?

Community
  • 1
  • 1
Bhuwan Pandey
  • 514
  • 1
  • 6
  • 19