drupal7配合cloudflare使用ssl模式不加载css的解决办法

从去年开始就发现一个安装了drupal系统的站点出现了没有加载css状况,查找发现说是ssl的问题,一直以为是证书问题或空间问题,也没有时间摆弄,今天有空仔细看看,发现cloudflare的SSL是没有问题的,因为通过源码发现的确是部分css加载使用了无https的地址,而站点整体是https加载,另外其他非drupal的站点https正常,所以应该是drupal的问题,drupal网站上有好多这样的讨论,一个个试验过都不行,只有方法7可行,具体过程如下:

方法1)直接放在开头<pho? 之后的,经过测试,不行!

if ( (!array_key_exists('HTTPS', $_SERVER)) && (PHP_SAPI !== 'cli') ) {

  if (substr($_SERVER['HTTP_HOST'], 0, 4) <> 'www.') {

    $new_url = 'www.' . $_SERVER['HTTP_HOST'];

  } else {

    $new_url = $_SERVER['HTTP_HOST'];

  }

  $new_url .= $_SERVER['REQUEST_URI'];

  header('HTTP/1.1 301 Moved Permanently');

  header('Location: https://'. $new_url);

  exit();

}

方法2)地址:https://www.drupal.org/https-information

如果您无权访问主服务器配置文件,并且必须在 .htaccess 文件中执行此任务,则使用 RewriteRule 是合适的:

RewriteCond %{HTTPS} off [OR]

RewriteCond %{HTTP_HOST} ^www\.example\.com*

RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

RewriteCond %{HTTPS} off [OR]

RewriteCond %{HTTP_HOST} ^www\.eastfong\.com*

RewriteRule ^(.*)$ https://eastfong.com/$1 [L,R=301]

.htaccess 中已有注释解释了如何将 http://example.com 重定向到 http://www.example.com(反之亦然),但此处的代码将这两个注释重定向到 https://example.com。

方法3)解决思路

将所有请求重定向到 https://www.url.de

我正在将https添加到drupal多站点安装中。HTTPS应该在所有URL上强制使用,HTTP不再可能。你可以得到这个:

# uncomment the following:

   #1

   RewriteCond %{HTTP_HOST} .

   RewriteCond %{HTTP_HOST} !^www\. [NC]

   RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

   RewriteCond %{eastfong.com} .

   RewriteCond %{eastfong.com} !^www\. [NC]

   RewriteRule ^ https://www.%{eastfong.com}%{REQUEST_URI} [L,R=301]

  

   # 2 Redirect to HTTPS

   RewriteCond %{HTTPS} off

   RewriteCond %{HTTP:X-Forwarded-Proto} !https

   RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

   RewriteCond %{HTTPS} off

   RewriteCond %{HTTP:X-Forwarded-Proto} !https

   RewriteRule ^(.*)$ https://%{eastfong.com}%{REQUEST_URI} [L,R=301] 

#1 是标准 htAccess 指令的修改版本,#2 取自 Drupal 8 HTeAccess。

方法4)解决地址:https://www.fastcomet.com/tutorials/drupal8/enable-ssl

查找代码行:

RewriteEngine on

并在其下方添加此代码:

# Redirect to HTTPS

RewriteCond %{HTTPS} off

RewriteCond %{HTTP:X-Forwarded-Proto} !https

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off

RewriteCond %{HTTP:X-Forwarded-Proto} !https

RewriteRule ^(.*)$ https://%{eastfong.com}%{REQUEST_URI} [L,R=301]

如果您有其他自定义重写规则,并且希望独立于此规则工作,也可以将此行添加到 .htaccess 文件的末尾。

恭喜,您已成功启用与网站的安全连接。

方法5)解决方式5地址

https://www.a2hosting.com/kb/installable-applications/optimization-and-configuration/drupal2/enabling-ssl-for-drupal

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.

RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} !^www\.

RewriteRule ^(.*)$ https://www.%{eastfong.com}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{eastfong.com}%{REQUEST_URI} [R=301,L]

方法6)解决方式6地址

https://www.ssldragon.com/how-to/http-to-https/drupal/

RewriteCond %{HTTP_HOST} !^www. [NC] RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP:X-Forwarded-Proto} !https

RewriteCond %{HTTPS} offRewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} !^www. [NC] RewriteRule ^ https://www.%{eastfong.com}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP:X-Forwarded-Proto} !https

RewriteCond %{HTTPS} offRewriteRule ^ https://%{eastfong.com}%{REQUEST_URI} [L,R=301]

方法7)以上对我都没有作用,解决的最后方法是:修改settings.php(因为此文件权限444,修改费了点时间,先使用ftp软件修改文件属性777,然后在线编辑内容,再ftp文件修改属性444)

具体修改为查找$base_url = ' 去掉前面"#"或直接在下面添加

$base_url = 'https://'.$_SERVER['SERVER_NAME'];

这个是非drupal官方的其他站长论坛看到的,注意修改了,忘记地址了,因为清楚缓存等,习惯的把记录清除掉了,搜了半天没有找到,只能默默感谢这个论坛了!

更新:终于找到了,地址:https://drupal.stackexchange.com/questions/169321/how-to-force-drupal-7-to-load-https-assets

You may also like

  • 通过CSS 定义字母的大小写,drupal
  • 查看DISCUZ隐藏个人空间的发帖记录
  • xampp环境安装Drupal10提醒PHP OPcode caching 未启用
  • 也在win7的右键菜单栏上添加“在此处打开Powershell”方便composer
  • wordpress总更新受不了,避免总是更新一个插件就解决了
  • Discuz X3.4 搬站换空间过程
  • vestacp恢复备份文件ssh端命令
  • Debian设置免密码sudo的方法