7

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" />

(etc.)

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?

10

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])) {
            continue;
        }

        $vars[$replace] = preg_replace('/(src|href|@import )(url\(|=)(")http(s?):/', '$1$2$3', $vars[$replace]);
    }
}
4
  • 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 '14 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 '15 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 ? – Mike Vranckx Jul 6 '15 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. – coredumperror Nov 2 '16 at 0:44
2

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

-1

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.

1
  • 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 '13 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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