 core/includes/file.inc                             |   27 ++++++++++++++++
 .../lib/Drupal/editor/Form/EditorImageDialog.php   |    6 +++-
 core/modules/filter/filter.module                  |    7 ++--
 .../Drupal/system/Tests/File/UrlRewritingTest.php  |   34 +++++++++++++++++---
 4 files changed, 64 insertions(+), 10 deletions(-)

diff --git a/core/includes/file.inc b/core/includes/file.inc
index 6065dd4..2a43edc 100644
--- a/core/includes/file.inc
+++ b/core/includes/file.inc
@@ -428,6 +428,7 @@ function file_stream_wrapper_get_instance_by_scheme($scheme) {
  *   could not be found to generate an external URL, then FALSE is returned.
  *
  * @see http://drupal.org/node/515192
+ * @see file_url_transform_relative()
  */
 function file_create_url($uri) {
   // Allow the URI to be altered, e.g. to serve a file from a CDN or static
@@ -470,6 +471,32 @@ function file_create_url($uri) {
 }
 
 /**
+ * Transforms an absolute URL of a local file to a relative URL.
+ *
+ * May be useful to prevent problems on multisite set-ups and prevent mixed
+ * content errors when using HTTPS + HTTP.
+ *
+ * @param string $file_url
+ *   A file URL of a local file as generated by file_create_url().
+ *
+ * @return string
+ *   If the file URL indeed pointed to a local file and was indeed absolute,
+ *   then the transformed, relative URL to the local file. Otherwise: the
+ *   original value of $file_url.
+ *
+ * @see file_create_url()
+ */
+function file_url_transform_relative($file_url) {
+  $scheme = \Drupal::request()->getScheme();
+  $port = \Drupal::request()->getPort();
+  debug($scheme);
+  debug($port);
+  debug(('http' == $scheme && $port == 80));
+  debug(\Drupal::request()->getHttpHost());
+  return preg_replace('|^https?://' . \Drupal::request()->getHttpHost() . '|', '', $file_url);
+}
+
+/**
  * Checks that the directory exists and is writable.
  *
  * Directories need to have execute permissions to be considered a directory by
diff --git a/core/modules/editor/lib/Drupal/editor/Form/EditorImageDialog.php b/core/modules/editor/lib/Drupal/editor/Form/EditorImageDialog.php
index d7ef08c..7367440 100644
--- a/core/modules/editor/lib/Drupal/editor/Form/EditorImageDialog.php
+++ b/core/modules/editor/lib/Drupal/editor/Form/EditorImageDialog.php
@@ -199,7 +199,11 @@ public function submitForm(array &$form, array &$form_state) {
     // attributes.
     if (!empty($form_state['values']['fid'][0])) {
       $file = file_load($form_state['values']['fid'][0]);
-      $form_state['values']['attributes']['src'] = file_create_url($file->getFileUri());
+      $file_url = file_create_url($file->getFileUri());
+      // Transform absolute image URLs to relative image URLs: prevent problems
+      // on multisite set-ups and prevent mixed content errors.
+      $file_url = file_url_transform_relative($file_url);
+      $form_state['values']['attributes']['src'] = $file_url;
       $form_state['values']['attributes']['data-editor-file-uuid'] = $file->uuid();
     }
 
diff --git a/core/modules/filter/filter.module b/core/modules/filter/filter.module
index ba0df51..e512ade 100644
--- a/core/modules/filter/filter.module
+++ b/core/modules/filter/filter.module
@@ -1378,10 +1378,9 @@ function _filter_html_image_secure_process($text) {
   $images = $html_dom->getElementsByTagName('img');
   foreach ($images as $image) {
     $src = $image->getAttribute('src');
-    // Remove absolute URLs pointing to the local domain to prevent mixed
-    // content errors.
-    $request = \Drupal::request();
-    $image->setAttribute('src', preg_replace('|^https?://' . $request->getHttpHost() . '|', '', $src));
+    // Transform absolute image URLs to relative image URLs: prevent problems on
+    // multisite set-ups and prevent mixed content errors.
+    $image->setAttribute('src', file_url_transform_relative($src));
 
     // Verify that $src starts with $base_path.
     // This also ensures that external images cannot be referenced.
diff --git a/core/modules/system/lib/Drupal/system/Tests/File/UrlRewritingTest.php b/core/modules/system/lib/Drupal/system/Tests/File/UrlRewritingTest.php
index 8e63a7e..d7a0e76 100644
--- a/core/modules/system/lib/Drupal/system/Tests/File/UrlRewritingTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/File/UrlRewritingTest.php
@@ -2,7 +2,7 @@
 
 /**
  * @file
- * Definition of Drupal\system\Tests\File\UrlRewritingTest.
+ * Contains Drupal\system\Tests\File\UrlRewritingTest.
  */
 
 namespace Drupal\system\Tests\File;
@@ -28,7 +28,7 @@ public static function getInfo() {
   }
 
   /**
-   * Test the generating of rewritten shipped file URLs.
+   * Tests the rewriting of shipped file URLs by hook_file_url_alter().
    */
   function testShippedFileURL()  {
     // Test generating an URL to a shipped file (i.e. a file that is part of
@@ -63,10 +63,10 @@ function testShippedFileURL()  {
   }
 
   /**
-   * Test the generating of rewritten public created file URLs.
+   * Tests the rewriting of public managed file URLs by hook_file_url_alter().
    */
-  function testPublicCreatedFileURL() {
-    // Test generating an URL to a created file.
+  function testPublicManagedFileURL() {
+    // Test generating an URL to a managed file.
 
     // Test alteration of file URLs to use a CDN.
     \Drupal::state()->set('file_test.hook_file_url_alter', 'cdn');
@@ -87,4 +87,28 @@ function testPublicCreatedFileURL() {
     $url = file_create_url($uri);
     $this->assertEqual('/' . base_path() . '/' . $public_directory_path . '/' . drupal_basename($uri), $url, 'Correctly generated a protocol-relative URL for a created file.');
   }
+
+  /**
+   * Test file_url_transform_relative().
+   */
+  function testRelativeFileURL() {
+    // Disable file_test.module's hook_file_url_alter() implementation.
+    \Drupal::state()->set('file_test.hook_file_url_alter', NULL);
+
+    // Shipped file.
+    $filepath = 'core/assets/vendor/jquery/jquery.js';
+    $url = file_create_url($filepath);
+    debug($url);
+    debug(base_path());
+    $this->assertIdentical(base_path() . $filepath, file_url_transform_relative($url));
+
+    // Managed file.
+    $uri = $this->createUri();
+    $url = file_create_url($uri);
+    debug($url);
+    $public_directory_path = file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath();
+    debug($public_directory_path);
+    $this->assertIdentical(base_path() . $public_directory_path . '/' . rawurlencode(drupal_basename($uri)), file_url_transform_relative($url));
+  }
+
 }
