16

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>
1
  • 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, 2013 at 19:44

8 Answers 8

34
+50

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>
5
  • 2
    Where is the role could be placed? in web.config file on the root?
    – SaidbakR
    Apr 22, 2014 at 0:34
  • 4
    @David Martin, what about {https}://www.domain.com to {https}://domain.com
    – MeTe-30
    Feb 23, 2015 at 16:48
  • @RohitArora I'm not sure I fully understand your issue, can I suggest you post a new question? Sep 2, 2015 at 8:58
  • @DavidMartin In short its not working for me. I also want the same for IIS 8. Thanks for your reply! Sep 2, 2015 at 10:21
  • 3
    If you want the redirect to stay on the same page (keep the path info) instead of going to the website root, change the action into: <action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
    – Mart
    Dec 10, 2017 at 20:18
5

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>
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

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.

1

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

1
  • This is incorrect, as the question talks about HTTPS.
    – Lex Li
    Mar 18 at 7:45
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.

1
  • 1. Changing bindings does not require service restart. 2. You haven't tried enough.
    – Lex Li
    Mar 18 at 7:43
-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>
-1

Redirecting https://www.example.com to https://example.com I had to do 2 steps

  1. Select the website, click "URL Rewrite" -> Click "Add Rule" -> Choose "Cononcial domain name" -> select your domain (without the www) -> make sure rule is at the top -> highlight the rule and click "Edit" -> and update "Redirect URL" at the end of the rule to include the 's' - https://example.com/{R:1}

  2. I had to also create a new website with the domain www.example.com, new app pool and select the SSL cert and point it to a folder containing the following (for some reason StackOverflow wont display the code I pasted below for the rewrite rule but just google IIS rewrite rule)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect to https" stopProcessing="true">
       <match url=".*" />
       <conditions>
         <add input="{HTTPS}" pattern="off" ignoreCase="true" />
       </conditions>
       <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
     </rule>
     <rule name="NonWwwRedirect"  stopProcessing="true"> 
        <match url="(.*)" /> 
        <conditions> 
            <add input="{HTTP_HOST}" pattern="^www.example\.com$" /> 
        </conditions> 
        <action type="Redirect" url="https://example.com/{R:1}" /> 
    </rule> 
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

After doing both steps only then does it work as desired

1
  • You should read the Markdown guide so as to learn the syntax to format code blocks. Besides, you don't usually need a new web site but add new bindings to your existing site.
    – Lex Li
    Mar 18 at 7:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.