简体   繁体   English

iis url 重定向 http 到非 www https

[英]iis url redirect http to non www https

i need to redirect from我需要重定向

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

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

http://domain.de to https://domain.de -does not work http://domain.dehttps://domain.de -不起作用

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>

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.我认为这对您有用,搜索模式具有可选的 www 并使用反向引用 C:2 进行重定向,该规则的条件是仅针对非 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>

The accepted answer doesn't handle the special case https://www.domain.de .接受的答案不处理特殊情况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>

If you want to redirect www to non www:如果要将 www 重定向到非 www:

  1. Add DNS entry for www.yourdomain.com to refer to your server's public IP为 www.yourdomain.com 添加 DNS 条目以引用您服务器的公共 IP
  2. Then you need to Edit Bindings of your website and "Add Binding" www.yourdomain.com然后您需要编辑您网站的绑定并“添加绑定”www.yourdomain.com
  3. Add a rewrite rule to your website using iis:使用 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参考: http : //madskristensen.net/post/url-rewrite-and-the-www-subdomain

If you want something more flexible than for your three examples, change your HTTP_HOST pattern to : \\w+\\.\\w+$ .如果您想要比三个示例更灵活的东西,请将您的 HTTP_HOST 模式更改为: \\w+\\.\\w+$ That would work for all three examples plus anything else, like subdomain.abcdef.domain.de .这适用于所有三个示例以及其他任何内容,例如subdomain.abcdef.domain.de

If you use this regex either encase it in parenthesis or change C:1 to C:0 in your action.如果您使用此正则表达式,请将其括在括号中或在您的操作中将 C:1 更改为 C: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我从: https : //medium.com/iis-and-windows-server/redirect-url-from-www-to-non-www-in-iis7-5-4d2909b9704

Redirecting https://www.example.com to https://example.com I had to do 2 stepshttps://www.example.com重定向到https://example.com我必须做 2 个步骤

  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}选择网站,点击“URL重写”->点击“添加规则”->选择“Cononcial域名”->选择您的域名(不带www)->确保规则在顶部->突出显示规则并点击“编辑”-> 并更新规则末尾的“重定向 URL”以包含 '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)我还必须创建一个域为www.example.com的新网站,新的应用程序池并选择 SSL 证书并将其指向包含以下内容的文件夹(出于某种原因,StackOverflow 不会显示我在下面粘贴的代码以用于重写规则但只是谷歌IIS重写规则)

After doing both steps only then does it work as desired完成这两个步骤后,它才能按预期工作

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:右键单击域并添加站点绑定,再添加一个带有 www 的域:

在此处输入图像描述

2. Restart services. 2.重启服务。 it should work.它应该工作。

These rewrite rules matches the following URL's:这些重写规则与以下 URL 匹配:

They will all redirect to: https://example.com他们都会重定向到: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM