dannycodes dannycodes - 1 month ago 17
CSS Question

Change Drupal 7 compiled css and js includes to use https as opposed to http

In Drupal 7 the css and javascript files that are compiled together and then added in the header are coming in as 'http://www.example.com/sites/all/css' and I need it to come in as 'https://www.example.com/sites/all/css'.

I've been able to achieve this by changing the baseUrl in settings.php to use https, but then it throws off our site. We are using Drupal as our CMS, and another framework as our LMS. So when the site loads the Drupal baseUrl from our LMS the address doesn't work.

Examples:

If I set baseUrl = 'https://www.example.com/cms' then the css loads properly as 'https://www.example.com/cms/sites/all/css' but then the Drupal admin site fails to load the css because the proper link from there is actually 'https://www.example.com/drupal/sites/all/css'

Same problem happens if I swap the baseUrl to = 'https://www.example.com/drupal'. This way the css doesn't load in our front end, but works properly in the Drupal admin side.

I'm wondering if there is a way to do a generic wildcard baseUrl that just says to use https like baseUrl = 'https:// %' or pulls the current url in the address bar every time to see what the generated url should look like.

I know this is very vague, but I don't know where/how else to ask.

Answer

I don't really understand your site structure using multiple subdirectories (/, /drupal, /cms) but what you could do is rewrite the resource URL's and removing the protocol.

In a custom module, implement the YOUR_MODULE_process_html hook and remove all protocols from the CSS & JS includes.

http://www.example.com/css/... will be transformed to //www.example.com/css/...

function YOUR_MODULE_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]);
    }
}