混合内容/不安全内容SSL

时间:2015-09-18 11:02:45

标签: wordpress apache .htaccess ssl mixed-content

我目前遇到以下问题

Mixed Content: The page at 'https://www.example.com/' was loaded over HTTPS, but requested an insecure stylesheet

这是安装了httpd的Centos服务器上的Wordpress网站。

我在`http.conf:

中有以下虚拟主机设置
NameVirtualHost *:80
NameVirtualHost *:443


<VirtualHost *:443>
    DocumentRoot /var/www/html/example
    ServerName www.example.com
    ServerAlias example.com
    SSLEngine on
    SSLCACertificateFile /etc/httpd/conf/ssl.crt/intermediate.crt
    SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt
    SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    Redirect / https://www.example.com/
</VirtualHost>

在我的httpd.conf我已将AllowOverride更改为全部,所以看起来如此:

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

我可以确认htaccess正在运行,因为我正在使用iTheme安全插件,这正在按预期工作,如果我在htacces中输入一些垃圾,我会收到服务器错误配置错误。

我更改了信息中心中的两个Wordpress网址,使用https代替http

完成所有这些后,我就可以通过HTTP访问该站点,被重定向到该站点的HTTPS版本并查看该站点。但是在控制台中我收到有关混合内容的错误,挂锁屏蔽显示为黄色或红色,而不是所需的绿色。

有一些文件是个问题我知道,例如我可以手动更改网址以使用https而不是http。据我了解,我可以使用更改下面的URL,这将只是调整当前使用的协议的链接:

<img src="//www.example.com/image.jpg" />

我还看到,如果https上的资源不可用,我只需执行以下操作:

https://example.com/imageserver?url=http://otherdomain.com/someimage.jpg&hash=abcdeafad

然而,我试图找到一种方法来使用htaccess一次性解决所有这些问题(我确信之前已做过这件事,但我的代码片段对我不起作用)。

有两个主要的片段我试图强迫https上的所有内容,第一个是:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

#These Lines to force HTTPS
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

第二个来自Dave Walsh:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

然而,似乎都没有解决我的问题。作为预防措施,我在每次更改后都重新启动httpd服务,即使htaccess更改也不需要重新启动,但情况仍然相同。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:3)

最简单的解决方案是使用下面的解决方案手动替换所有链接将节省您的时间并且非常直接。

我们的想法是删除所有(协议HTTP和HTTPS)并让它们使用协议相对URL https://stackoverflow.com/a/15146073/3599237

我们可以使用以下index.php

代码执行此操作
<?php
//this lined added here
ob_start();
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

//and these lines also 
$output = ob_get_contents();
ob_end_clean();

$output = str_replace(array("https://", "http://"), "//", $output);
echo str_replace('http:\/\/', "\/\/", $output);

更新:您只需使用内容安全政策

即可
  

HTTP内容 - 安全 - 策略(CSP)升级 - 不安全请求   指令指示用户代理处理所有站点的不安全URL   (通过HTTP提供的服务)好像已被安全替换   网址(通过HTTPS提供的网址)。该指令适用于Web   需要具有大量不安全遗留URL的站点   重写。

     

之前评估了upgrade-insecure-requests指令   block-all-mixed-content如果设置了,后者实际上是a   无操作。建议设置任一指令,但不能同时设置两者,除非   你想在旧的浏览器上强制使用HTTPS,而这些浏览器不会强制它   重定向到HTTP。

将以下行放入标题部分(header.php文件)。

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

有关详细信息,请参阅:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/upgrade-insecure-requests