【问题标题】:Rewrite Rule causes "partially encrypted connection" in Firefox & Chrome重写规则导致 Firefox 和 Chrome 中的“部分加密连接”
【发布时间】:2012-02-15 15:47:12
【问题描述】:

问题基于问题htaccess rewriterule: redirect http to https or https to http (both ways) depending on URL typed。很棒的解决方案(感谢 Ulrich Palha)如下所示:

RewriteEngine on
RewriteBase /

#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]


#all pages that are supposed to be http redirected if https
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]

#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]

不幸的是,一旦我将此代码添加到 .htaccess 文件中,一切正常,但 https 开始看起来已损坏,如果包含指向 css 或来自服务器的图片的链接,尽管路径是相对的。

以下页面 (https://www.example.com/?p=welcome) 显示 SSL 连接断开,FF 浏览器显示“您与此站点的连接仅部分加密”

<head>
<title>ddd</title>
</head>
<body>
Hello
<img src="/gfx/stars/help.gif" alt="" /> 
</body>
</html>

为什么?

一旦我从页面中删除&lt;img src="/gfx/stars/help.gif" alt="" /&gt;,FF 显示 https 正常,然后 URL 栏为绿色(显示 SSL 证书)。当我使用 Google Chrome 进行测试时,出现了同样的错误。

那么,&lt;img src="/gfx/stars/help.gif" alt="" /&gt; 有什么问题?路径是相对的。 如果我添加也会发生同样的情况

<link rel="stylesheet" type="text/css" href="/css/mycss.css" />

【问题讨论】:

    标签: .htaccess mod-rewrite https path relative-path


    【解决方案1】:

    一旦页面加载完毕并且浏览器开始通过 https 请求图片等。通过我阅读您的重写规则,因为图像的路径没有查询字符串 IS_HTTP 设置为 1。这会导致您的重写规则重定向到图像 URL 的 http 版本,这就是浏览器抱怨的原因

    【讨论】:

      【解决方案2】:

      解决此问题的一种方法是避免处理资源(因为无论如何您都没有任何特殊规则),如下所示

      RewriteEngine on
      RewriteBase /
      
      #if its a resource (add others that are missing)
      RewriteCond %{REQUEST_URI} \.(gif|css|png|js|jpe?g)$ [NC]
      #do nothing
      RewriteRule ^ - [L]
      
      #rest of existing rules go here
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多