diff --git a/advagg.inc b/advagg.inc
index 41c372b..92af20d 100644
--- a/advagg.inc
+++ b/advagg.inc
@@ -665,10 +665,32 @@ function advagg_create_aggregate_files($plans) {
  *   Name of the stylesheet to be processed.
  * @param $optimize
  *   Defines if CSS contents should be compressed or not.
+ * @param $aggregate_settings
+ *   array of settings.
  * @return
  *   Contents of the stylesheet, including any resolved @import commands.
  */
-function advagg_load_css_stylesheet($file, $optimize) {
+function advagg_load_css_stylesheet($file, $optimize, $aggregate_settings = NULL) {
+  // Use current $aggregate_settings if none was passed in.
+  if (is_null($aggregate_settings)) {
+    $aggregate_settings = advagg_current_hooks_hash_array();
+  }
+
+  // Set http/https context.
+  $orginal_base_root = $GLOBALS['base_root'];
+  $orginal_base_url = $GLOBALS['base_url'];
+  $orginal_is_https = $GLOBALS['is_https'];
+  $GLOBALS['is_https'] = $aggregate_settings['variables']['is_https'];
+  if ($aggregate_settings['variables']['is_https']) {
+    $GLOBALS['base_root'] = str_replace('http://', 'https://', $GLOBALS['base_root']);
+    $GLOBALS['base_url'] = str_replace('http://', 'https://', $GLOBALS['base_url']);
+  }
+  else {
+    $GLOBALS['base_root'] = str_replace('https://', 'http://', $GLOBALS['base_root']);
+    $GLOBALS['base_url'] = str_replace('https://', 'http://', $GLOBALS['base_url']);
+  }
+
+  // Get the stylesheets contents.
   $contents = drupal_load_stylesheet($file, $optimize);
 
   // Build the base URL of this CSS file: start with the full URL.
@@ -685,5 +707,11 @@ function advagg_load_css_stylesheet($file, $optimize) {
   // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths.
   $contents = preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents);
 
+  // Set the http/https context back.
+  $GLOBALS['base_root'] = $orginal_base_root;
+  $GLOBALS['base_url'] = $orginal_base_url;
+  $GLOBALS['is_https'] = $orginal_is_https;
+
+  // Return the stylesheets contents.
   return $contents;
 }
diff --git a/advagg.missing.inc b/advagg.missing.inc
index c46d3f7..db3f155 100644
--- a/advagg.missing.inc
+++ b/advagg.missing.inc
@@ -352,7 +352,7 @@ function advagg_get_css_aggregate_contents($files, $aggregate_settings) {
     $import_statements = array();
     module_load_include('inc', 'advagg', 'advagg');
     foreach ($files as $file => $settings) {
-      $contents = advagg_load_css_stylesheet($file, $optimize);
+      $contents = advagg_load_css_stylesheet($file, $optimize, $aggregate_settings);
 
       // Allow other modules to modify this files contents.
       // Call hook_advagg_get_css_file_contents_alter().
diff --git a/advagg_mod/advagg_mod.module b/advagg_mod/advagg_mod.module
index 034de22..b540761 100644
--- a/advagg_mod/advagg_mod.module
+++ b/advagg_mod/advagg_mod.module
@@ -343,7 +343,7 @@ function advagg_mod_inline_css(&$css) {
 
     $file = $values['data'];
     if (file_exists($file)) {
-      $contents = advagg_load_css_stylesheet($file, $optimize);
+      $contents = advagg_load_css_stylesheet($file, $optimize, $aggregate_settings);
 
       // Allow other modules to modify this files contents.
       // Call hook_advagg_get_css_file_contents_alter().
