1
votes

Suppose I have a site http://www.example.com which was 301 redirect from http://example.com to http://www.example.com.

Now after Google's recent update about using SSL for better rankings, I decided to use SSL.

I am using below code in web.config to 301 redirect to https

 <rewrite>
            <rules>
                <rule name="Redirect to HTTPS" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://www.example.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>

now its redirecting 301 all requests http://www.example.com and http://example.com to https://www.example.com

but it's not redirecting https://example.com to https://www.example.com

I want to do this for only one canonical version as many SEO authority sites suggested.

If I use Plesk builtin functionality to redirect all non www to www, then it does double redirect.

How can I do this, Please help

1

1 Answers

0
votes

Try a second rule to enforce the www, such as:

<rule name="Enforce WWW" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{CACHE_URL}" pattern="^https://(?!www)(.*)" />
  </conditions>
  <action type="Redirect" url="https://www.{C:1}" redirectType="Permanent" />
</rule>

The condition you have on your rule will stop it firing for a https request such as https://example.com.


Version #2 Try putting this before your other rule, the stopProcessing should stop it going circular.

<rule name="Enforce WWW" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="example.com" />
    </conditions>
    <action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>