17

i need to redirect from

www.domain.de to https://domain.de -works

http://www.domain.de to https://domain.de -works

http://domain.de to https://domain.de -does not work

rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="false" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
      </conditions>
      <action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Marius Krämer
  • 446
  • 1
  • 7
  • 19
  • i assume that rewrite is properly inside its opening tags, even though here in code the opening chevron is missing. and can you elaborate "Imported Rule 1" ? – Roshan Jun 16 '13 at 19:44

7 Answers7

34

I think this will work for you, the search pattern has the optional www and redirects using the back reference C:2, the rule has a condition to only run against non https.

This is the pattern:

"^(www\.)?(.*)$"

where:

{C:0} - www.domain.de
{C:1} - www.
{C:2} - domain.de

Here's the rule in full:

  <rewrite>
    <rules>
      <rule name="SecureRedirect" stopProcessing="true">
        <match url="^(.*)$" />
        <conditions>
          <add input="{HTTPS}" pattern="off" />
          <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
        </conditions>
        <action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
David Martin
  • 11,764
  • 1
  • 61
  • 74
7

The accepted answer doesn't handle the special case https://www.domain.de.

These rules do the complete job:

    <rewrite>
      <rules>
        <rule name="Redirect to HTTPS without www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="^OFF$" />
            <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
        </rule>
        <rule name="Special case for HTTPS with www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="^ON$" />
            <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
mrmashal
  • 1,721
  • 18
  • 21
4

If you want to redirect www to non www:

  1. Add DNS entry for www.yourdomain.com to refer to your server's public IP
  2. Then you need to Edit Bindings of your website and "Add Binding" www.yourdomain.com
  3. Add a rewrite rule to your website using iis:
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
  <match url="*" />
  <conditions>
    <add input="{CACHE_URL}" pattern="*://www.*" />
  </conditions>
  <action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>

Reference: http://madskristensen.net/post/url-rewrite-and-the-www-subdomain

MarwaAhmad
  • 808
  • 9
  • 21
2

If you want something more flexible than for your three examples, change your HTTP_HOST pattern to : \w+\.\w+$. That would work for all three examples plus anything else, like subdomain.abcdef.domain.de.

If you use this regex either encase it in parenthesis or change C:1 to C:0 in your action.

Daniel Gimenez
  • 18,530
  • 3
  • 50
  • 70
0

This is what worked for me:

    <rule name="NameRule1" stopProcessing="true" enabled="true" >
        <match url="(.*)" />
        <conditions>
          <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
        </conditions>
        <action type="Redirect" url="http://example.com/{R:1}" />
    </rule>

I got it from: https://medium.com/iis-and-windows-server/redirect-url-from-www-to-non-www-in-iis7-5-4d2909b9704

Hashim Akhtar
  • 813
  • 2
  • 11
  • 16
0

Tried many of solutions but below works for me:

  1. Right click on Domain and Add a Site bindings, add one more domain with www:

enter image description here

2. Restart services. it should work.

Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
-1

These rewrite rules matches the following URL's:

They will all redirect to: https://example.com

These rewrite rules may redirect twice because of the separate rules. (I'm a newbie with regex)

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="*" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
                </rule>
                <rule name="WWW" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
                    </conditions>
                    <action type="Redirect" url="https://example.com{PATH_INFO}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
Jamie
  • 3,031
  • 5
  • 36
  • 59