URL在某些URL上重写为https

问题描述:

我需要将http请求重定向到https。但只有当我的网站通过正常的URL访问。即www.accufinance.comURL在某些URL上重写为https

当我在本地调试中访问它时,我使用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,它可以正常工作,但会捕获所有连接。如何在网址中存在'accufinance.com'的情况下仅启用规则?

您需要检查HTTP_HOST,以符合您的域名的条件。
该指令matchurl param仅包含您的域名后面的内容。

例子: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> 

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

正则表达式^accufinance.com$是极其严格的。 ^表示它必须一直匹配到开始(即没有其他任何东西在它之前),并且$要求没有任何东西跟着它一直到最后。因此,只有当请求URL正好是accufinance.com时,匹配才会成功。

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

+0

Thanks!我试过这个 - - 但仍然没有重定向到https。我需要'点'和'斜线'吗? – Craig 2014-09-20 01:02:59

+0

另外 - - 有效,但是......我不希望它在url为localhost或其他任何地方时重定向。 – Craig 2014-09-20 01:04:16