iis url redirect http to non www https

前端 未结 5 926
半阙折子戏
半阙折子戏 2020-12-03 05:42

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

相关标签:
5条回答
  • 2020-12-03 06:11

    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

    0 讨论(0)
  • 2020-12-03 06:17

    These rewrite rules matches the following URL's:

    • www.example.com
    • http://www.example.com
    • https://www.example.com

    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>
    
    0 讨论(0)
  • 2020-12-03 06:22

    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.

    0 讨论(0)
  • 2020-12-03 06:28

    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>
    
    0 讨论(0)
  • 2020-12-03 06:29

    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

    0 讨论(0)
提交回复
热议问题