【问题标题】:http to https issues .htaccess web.config global.asx windows hostinghttp 到 https 问题 .htaccess web.config global.asx windows 托管
【发布时间】:2023-03-22 12:43:01
【问题描述】:

我想知道是否有人可以提供帮助。我们希望能够将我们的网站重定向到全球的 https 版本。已安装 SSL 证书,并且该站点的 https 版本处于活动状态且正在运行。我尝试了以下方法。

www 根目录中的 web.config。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="SSL" stopProcessing="true">
                <match url="^(.*)$"  />
                    <conditions>
                        <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true"/>
                    </conditions>
                <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

www 根目录中的.htaccess,并在托管服务提供商上启用了 URL 重写。

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://mysitedomain.com/$1 [R=301,L]

我在更改和删除浏览器缓存之后回收了应用程序池。两种方法都不起作用。是否有任何一种方法可以尝试验证任何一种方法以确保文件正在被读取,因为它似乎没有做任何事情。是否可以尝试使用全球 asa ?如果您需要任何进一步的信息,请询问。

已编辑以提供有关托管环境的更多信息。 我们正在使用运行 Windows 2003 的共享云托管平台(这很容易在不同平台之间切换),并且通过 Web 门户访问。该站点运行自己的应用程序池,我们可以对其进行回收。有激活和停用 URL 重写的选项。这里的描述。

启用“重写”模块将允许您在 Windows 或混合模式网站上的 .htaccess 文件中使用 Apache mod_rewrite 样式功能。例如,可以将 /store/products/televisions 等 URL 重写为 /store.php?type=products&category=televisions 以优化搜索引擎。有关详细信息,请参阅以下外部 URL。

我能够逐页硬编码 SSL 重定向,但在全球范围内,无论我尝试什么,无论是 web.config 还是 .htaccess 似乎都无法接受。

【问题讨论】:

  • 就像我说的 (在您删除上一个问题之前) 我们不知道您的架构/环境等。如果您只想将 http 流量重定向到 https,那么 @ 987654321@ 应该可以。需要回答的未涵盖的问题,正在运行什么版本的 Windows 操作系统和 IIS?您可以通过 VPS 访问还是仅通过网络面板访问?
  • 你还提到.htaccess文件has nothing to do with IIS hosted sites(它被Apache Web服务器使用),那么你甚至使用IIS吗?
  • @Lankymart 偶尔会发现 Helicon 的第三方 isapi 重写过滤器安装在 IIS 上,它与 Apache 风格的 .htaccess 文件一起工作。现在我真的不明白微软有一个本地重写模块的意义,但我猜有些人更喜欢它
  • @John 可能,但我不相信这种情况。第二个问题,关于他们的环境仍然缺乏足够的细节。

标签: .htaccess asp-classic web-config global.asa


【解决方案1】:

要将网站从HTTP 重定向到HTTPS,请尝试关注。

如果您使用的是Linux &amp; cPanel – Force HTTPS using .htaccess

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.domainname.com%{REQUEST_URI} [L,R=301]

如果你使用的是Windows &amp; Plesk – Force HTTPS using web.config

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

别忘了清除缓存

【讨论】:

  • global.asa 会干扰 web.config 吗?
  • @Emma 不,global.asa 用于声明应用程序和会话级别的事件处理程序以及声明全局类型库,它与 IIS 网站配置无关。
【解决方案2】:

可能有不同的方法可以做到这一点。只需像这样在脚本中重定向

<%
   If Request.ServerVariables("SERVER_PORT")=80 Then
      Dim strSecureURL
      strSecureURL = "https://"
      strSecureURL = strSecureURL & Request.ServerVariables("SERVER_NAME")
      strSecureURL = strSecureURL & Request.ServerVariables("URL")
      strSecureURL = strSecureURL & "?" & Request.ServerVariables("QUERY_STRING")
      Response.Redirect strSecureURL
   End If
%>

HOW TO: Use ASP to Force SSL for Specific Pages

【讨论】:

  • 谢谢亚历克斯,是的,我一直在这样做,但显然不如在全球范围内这样做干净。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 2010-12-21
  • 2018-03-30
  • 2012-06-16
相关资源
最近更新 更多