首页 文章

URL在某些URL上重写为https

提问于
浏览
1

我需要将http请求重定向到https . 但只有当我的网站通过普通网址访问时 . 即www.accufinance.com

当我在本地调试中访问它时,我使用localhost连接 - 并且不希望重写发生(因为我没有本地SSL) .

我在尝试这个:

<rewrite>
      <rules>
        <clear />
        <rule name="Redirect to https" stopProcessing="true">
          <match url="^accufinance.com$" ignoreCase="true"/>
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
        </rule>
      </rules>
    </rewrite>

但重写规则不会发生 . 如果我使用( . *)作为匹配URL,它可以正常工作,但捕获所有连接 . 如果URL中有'accufinance.com',我该怎么办才能触发规则?

2 回答

  • 2

    您需要检查 HTTP_HOST ,其条件与您的域名相匹配 .
    url param的指令 match 仅包含域名之后的内容 .

    示例: http://www.domain.tld/some/url/here.ext

    • HTTP_HOST = www.domain.tld

    • REQUEST_URI = /some/url/here.ext

    您可以使用此规则执行所需操作

    <rule name="Redirect to https" stopProcessing="true">
      <match url=".*" ignoreCase="true" />
        <conditions>
          <add input="{HTTP_HOST}" pattern="^(www\.)?accufinance\.com$" ignoreCase="true" />
          <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
    </rule>
    

    请注意,在再次尝试之前,您需要清除浏览器的缓存(由于您的永久重定向,旧规则仍在缓存中)

  • 2

    正则表达式 ^accufinance.com$ 极其严格 . ^ 表示它必须一直匹配到开头(即它之前没有其他内容),并且 $ 要求没有任何内容一直跟着它结束 . 因此,仅当请求URL完全是 accufinance.com 时,匹配才会成功 .

    尝试删除 ^$ . 或者,您可以在所需过滤器之前和之后明确允许部分URL,如 .*accufinance\.com.* .

相关问题