I'm trying to avoid serving pages that load css and js resources through URLs that specify either http or https. (I'm trying to find a workaround for the problem described in this question, in case a real solution cannot be found.)

Because Drupal is specifying http:// for the href for all the CSS and JS files, when the server erroneously switches to https, as described in the aforementioned question, the browser seems to be ignoring all these files, and I get an unstyled page.

Instead of

<link type="text/css" rel="stylesheet" href="http://www.example.com/sites/default/files/css/css_pbm0lsQQJ7A7WCCIMgxLho6mI_kBNgznNUWmTWcnfoE.css" media="all" />


I'd like to serve

<link type="text/css" rel="stylesheet" href="/sites/default/files/css/css_pbm0lsQQJ7A7WCCIMgxLho6mI_kBNgznNUWmTWcnfoE.css" media="all" />

or even

<link type="text/css" rel="stylesheet" href="//www.example.com/sites/default/files/css/css_pbm0lsQQJ7A7WCCIMgxLho6mI_kBNgznNUWmTWcnfoE.css" media="all" />

Can I make Drupal serve URLs that don't specify protocol?

3 Answers 3


You can use a template process (YOUR_THEME_process_html) hook to rewrite all resource urls (js and css) using a regular expression.

Here is an example which transforms all http:// or https:// to //:

function YOUR_THEME_process_html(&$vars)
    foreach (array('head', 'styles', 'scripts') as $replace) {
        if (!isset($vars[$replace])) {

        $vars[$replace] = preg_replace('/(src|href|@import )(url\(|=)(")http(s?):/', '$1$2$3', $vars[$replace]);
  • This worked like a charm for me. We too are behind an F5 server and are using it to make sure all requests are done through https.
    – user5013
    Jul 29, 2014 at 23:17
  • This worked for me, unfortunately it doesn't appear to affect img tags (even adding img to the array) so I'm still gettting issues with this. It's odd that Drupal will load all resources over HTTP even when it's HTTPS.
    – nbsp
    Jul 3, 2015 at 5:13
  • img are not part of the $vars variables passed from the YOUR_THEME_process_html. The images you are trying the alter the source URL, are they part of the content ? Jul 6, 2015 at 11:59
  • Worked like a charm! For those wondering how to apply this conversion to links in the page content, add 'page' to the array in the foreach loop. Nov 2, 2016 at 0:44

This has been fixed in Drupal 8. In Drupal 7, there is an issue about fixing it.


We had that same issue with a website we were developing and could not come to a resolution on why some of the paths would be https while others would be http. The work around, which you may not like the answer, was to use the Pathologic module which turns everything from an absolute to relative so your forms would use a /path-to-form instead of http://www.whatever.com/path-to-form.

  • 3
    Since pathologic is an input filter, it AFAIK only affects content, not the URLs that Drupal generates. So I don't see how this can help here either.
    – iconoclast
    Feb 28, 2013 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.