简体   繁体   English

IIS HTTP到HTTPS相对重定向

[英]IIS HTTP to HTTPS relative redirect

I recently got a SSL certificate for my website and want to redirect all traffic to HTTPS. 我最近获得了我的网站的SSL证书,并希望将所有流量重定向到HTTPS。 I got everything to go to https://mydomain.com but if someone enters http://mydomain.com/anotherpage it drops the other page and just takes the user to the home page. 我得到了一切去https://mydomain.com但如果有人进入http://mydomain.com/anotherpage它会删除另一页,只是将用户带到主页。

My rule in my web.config file looks like this: 我的web.config文件中的规则如下所示:

<rule name="HTTP to HTTPS redirect" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  </conditions>
  <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>

I also tried https://{HTTP_HOST}{REQUEST_URI} without any success. 我也试过https://{HTTP_HOST}{REQUEST_URI}但没有成功。 Can anyone tell me what I need to do to make the website redirect to the proper HTTPS version of the page? 任何人都可以告诉我我需要做些什么来使网站重定向到正确的HTTPS版本的页面? I have a feeling it has something to do with the pattern, but I can't seem to figure out the syntax. 我觉得它与模式有关,但我似乎无法弄清楚语法。

I found a way to do this, and you don't need the Rewrite module for it. 我找到了一种方法,你不需要Rewrite模块。
The following worked for me on Windows 8 (IIS 8.5): 以下适用于Windows 8(IIS 8.5):

  1. Remove the HTTP binding from your site (leave HTTPS in place) 从您的站点删除HTTP绑定(保留HTTPS)
  2. Add another site 添加其他网站
  3. Make sure that the new site has HTTP binding 确保新站点具有HTTP绑定
  4. Configure HTTP Redirect as shown: 配置HTTP重定向,如下所示:

在此输入图像描述

Now all HTTP request will redirect to your HTTPS site and will preserve the rest of the URL. 现在,所有HTTP请求都将重定向到您的HTTPS站点,并将保留URL的其余部分。

Change it to: 将其更改为:

<rewrite>
   <rules>
      <rule name="Redirect to HTTPS" stopProcessing="true">
         <match url="(.*)" />
         <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
         </conditions>
         <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
      </rule>
   </rules>
</rewrite>

I can't comment yet or I'd leave this as a comment under AndyH's answer. 我还不能发表评论,或者我会在AndyH的回答中留下这个评论。 The solution was correct, though I hit a single further snag (likely tied to the use of Adobe's Coldfusion server). 解决方案是正确的,虽然我遇到了一个进一步的障碍(可能与使用Adobe的Coldfusion服务器有关)。 I wanted to share some further research I had to do for any other unfortunate soul who may run into it. 我想分享一些进一步的研究,我必须为任何可能遇到它的其他不幸的灵魂做。

Once set up, the redirect would always end at this url: 设置完成后,重定向将始终以此URL结尾:

https://xxx.xxx.com/jakarta/isapi_redirect.dll

The fix for this was found in an Adobe thread ( https://forums.adobe.com/thread/1034854 ): I had to change an application pool's settings as follows: 对此的修复是在Adobe线程( https://forums.adobe.com/thread/1034854 )中找到的:我必须更改应用程序池的设置,如下所示:

Real site (HTTPS binding only, actually contains code and virtual directories) Application pool's Advanced Settings: Enable 32-Bit Applications : False 真实站点(仅限HTTPS绑定,实际上包含代码和虚拟目录)应用程序池的高级设置: Enable 32-Bit Applications : False

Http_Redirect site (HTTP binding only, is a blank shell of a folder with no directories) Application pool's Advanced Settings: Enable 32-Bit Applications : True Http_Redirect站点(仅限HTTP绑定,是没有目录的文件夹的空白shell)应用程序池的高级设置: Enable 32-Bit Applications : True


EDIT: Another detail, tied to query string preservation: 编辑:另一个与查询字符串保存相关的细节:

Per suggestion in this post ( http://www.developerfusion.com/code/4678/permanent-301-redirect-with-querystring-in-iis/ ) 根据这篇文章的建议( http://www.developerfusion.com/code/4678/permanent-301-redirect-with-querystring-in-iis/

Add $S$Q at the end of the domain and make sure the box for Redirect all requests to exact destination is checked. 在域末尾添加$S$Q ,并确保选中Redirect all requests to exact destination的框。 Then it will save the query string as well. 然后它也会保存查询字符串。

I had the same problem where the R:1 was dropping my folders. 我有同样的问题, R:1正在丢弃我的文件夹。 I fixed it like this. 我这样修好了。

<rule name="http to https" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
       <add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
            appendQueryString="false" redirectType="SeeOther" />
</rule>

I believe AndyH's answer to be the easiest and best way. 我相信AndyH的答案是最简单和最好的方式。 I have found using the URL rewrite can also conflict with code that may redirect the user to another page. 我发现使用URL重写也可能与可能将用户重定向到另一个页面的代码冲突。 IT commonly broke in our environment. IT通常在我们的环境中破裂。 But Andy's solution worked flawlessly. 但安迪的解决方案运作完美无缺。 I also think Andy's solution will put less overhead on the server as it doesn't need to examine every url hitting it for possible re-write conditions. 我还认为Andy的解决方案会减少服务器上的开销,因为它不需要检查每个URL是否有可能重写条件。


I found a workaround: 我找到了一个解决方法:

Consider what in IIS is consired a website: simply a set of rules, the path in which get files and its bindings. 考虑一下IIS中的一个网站:简单的一组规则,获取文件及其绑定的路径。

Furthermore, there's available a function called " HTTP Redirect " (included standardly in IIS), that redirect an host to another, keeping all subdirectory (it makes a relative path). 此外,还有一个名为“ HTTP重定向 ”的功能(在IIS中标准配置),它将主机重定向到另一个主机,保留所有子目录(它构成一个相对路径)。 The workaround is to leave just the binding for HTTPS (port 443) in your website, and create another with the binding on HTTP (port 80) and set for this an HTTP redirect to your URL with https:// . 解决方法是在您的网站中仅保留HTTPS(端口443)的绑定,并使用HTTP上的绑定(端口80)创建另一个绑定,并使用https://为您的URL设置HTTP重定向。

For example, consider a website called mytest and its urls http://www.mytest.com/ and https://www.mytest.com/ . 例如,考虑一个名为mytest的网站及其网址http://www.mytest.com/https://www.mytest.com/ Set for it instead only binding on https://www.mytest.com/ , and delete the http binding. 设置为仅在https://www.mytest.com/绑定,并删除http绑定。 Then create a new website with the same local path, called mytest http with just a binding over port 80 ( http://www.mytest.com/ ) and set for this one an HTTP Redirect to https://www.mytest.com/ . 然后创建一个具有相同本地路径的新网站,称为mytest http ,仅通过端口80( http://www.mytest.com/ )绑定,并为此设置HTTP Redirecthttps://www.mytest.com/
Simple and clean, and that should be as fast as directly the https url for the user, because it's just an internal redirect. 简单而干净,应该与用户的https url一样快,因为它只是一个内部重定向。 I hope that can work for you! 我希望能为你效劳!

You can add the URL Rewrite module to IIS (IIS 7 or higher) which allows you to add create the redirect in a visual way. 您可以将URL重写模块添加到IIS(IIS 7或更高版本),这允许您以可视方式添加创建重定向。 The module can be downloaded here . 该模块可以在这里下载。

This step-by-step tutorial worked wonders for me and explains that when using this module, all it actually does is add some code to your web.config file as such: 这个循序渐进的教程为我创造了奇迹并解释说,在使用这个模块时,它实际上所做的就是在web.config文件中添加一些代码:

<rewrite>
    <rules>
        <rule name="Redirect to HTTPS" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
        </rule>
    </rules>
</rewrite>

I have found that the 我发现了

<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />

syntax will only work for the website's ROOT web.config file. 语法仅适用于网站的ROOT web.config文件。

If the rewrite rule is applied to a virtual web.config file, then use.. 如果重写规则应用于虚拟web.config文件,则使用..

<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{URL}" />

The {URL} syntax will include the initial forward slash, the virtual path, and any URL parameters. {URL}语法将包括初始正斜杠,虚拟路径和任何URL参数。

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

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