重写规则导致Firefox中的“部分加密连接”。铬

时间:2012-01-21 10:21:27

标签: .htaccess mod-rewrite https path relative

问题基于问题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.mydomain.com/?p=welcome)显示SSL连接断开,FF浏览器显示“您与此站点的连接只是部分加密”

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

为什么?

从页面中删除<img src="/gfx/stars/help.gif" alt="" />后,FF显示https罚款,URL栏为绿色(显示SSL证书)。当我使用谷歌浏览器进行测试时,会出现同样的错误。

那么,<img src="/gfx/stars/help.gif" alt="" />出了什么问题?路径是相对的。 如果我添加

,也会发生同样的情况
<link rel="stylesheet" type="text/css" href="/css/mycss.css" />

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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