diff --git a/typo3/sysext/core/Classes/Resource/ResourceCompressor.php b/typo3/sysext/core/Classes/Resource/ResourceCompressor.php
index 91359ef0c14e939f481375a976699448ecc980b4..a8fbffbdd3ebc424cae580f48d78aceaeb75c59f 100644
--- a/typo3/sysext/core/Classes/Resource/ResourceCompressor.php
+++ b/typo3/sysext/core/Classes/Resource/ResourceCompressor.php
@@ -419,9 +419,16 @@ class ResourceCompressor
      */
     protected function getFilenameFromMainDir($filename)
     {
+        $docRoot = GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT');
+        $fileNameWithoutSlash = ltrim($filename, '/');
+
+        // if the file exists in the document root
+        if (is_file($docRoot . '/' . $fileNameWithoutSlash)) {
+            return substr($docRoot . '/' . $fileNameWithoutSlash, strlen($this->rootPath));
+        }
         // if the file exists in the root path, just return the $filename
-        if (is_file($this->rootPath . ltrim($filename, '/'))) {
-            return ltrim($filename, '/');
+        if (is_file($this->rootPath . $fileNameWithoutSlash)) {
+            return $fileNameWithoutSlash;
         }
         // if the file is from a special TYPO3 internal directory, add the missing typo3/ prefix
         if (is_file(realpath(PATH_site . TYPO3_mainDir . $filename))) {
@@ -433,7 +440,7 @@ class ResourceCompressor
         } elseif (strpos($filename, '../') === 0) {
             $file = GeneralUtility::resolveBackPath(PATH_typo3 . $filename);
         } else {
-            $file = PATH_site . ltrim($filename, '/');
+            $file = PATH_site . $fileNameWithoutSlash;
         }
 
         // check if the file exists, and if so, return the path relative to TYPO3_mainDir