Solving the “MIME Type Not Executable” Error in Magento
Hello, fellow developers! I recently encountered an issue during a fresh installation of Magento2 on my local Xampp setup. When attempting to access the admin panel after successfully running composer install and setting up the database, I faced this error:
Refused to execute script from '<URL>' because its MIME type ('text/html') is not executable.
The error message was rather cryptic, but with some troubleshooting, we can resolve it. Let’s dive into the solution.
Understanding the Error: Strict MIME Type Checking
This error occurs due to Magento’s strict security settings around MIME types for executable scripts. By default, Magento only allows certain MIME types (like application/x-php, application/octet-stream) to be executed on the server. When a file with an unrecognized or restricted MIME type is encountered during the execution of PHP code, this error is thrown.
The Failed Solution Attempt
Before we proceed, let me clarify why the suggested solution from the Magento StackExchange post didn’t work for you:
nano app/etc/env.php
The suggested edit to remove strict_mode or change its value isn’t a recommended approach. Modifying this setting can compromise your site’s security by disabling important checks.
The Correct Solution: Updating .htaccess
The error is actually due to an incorrect MIME type for PHP files in the server configuration. Here’s how we can fix it:
-
Navigate to your Magento installation directory using Xampp’s file manager or via command line.
-
Open the
.htaccessfile located in the root of your Magento installation. If you don’t see it, ensure that “Show hidden files” is enabled in your Xampp settings. -
Find the following lines:
<IfModule mod_headers.c>
<FilesMatch "\.(js|css|png|jpg|jpeg|gif|ico)$">
FileETag None
Header unset ETag
Header set Cache-Control "max-age=2016000, public"
ExpiresByType image/x-icon "access plus 1 year"
</FilesMatch>
</IfModule>
<IfModule mod_expire.c>
ExpiresDefault "access plus 7 days"
ExpiresActive On
# CSS and JS are often not modified but may require an expiration date
ExpireByType text/css "access plus 1 year"
ExpireByType application/javascript "access plus 1 year"
</IfModule>
- Replace the entire block with the following:
<IfModule mod_headers.c>
<FilesMatch "\.(js|css|png|jpg|jpeg|gif|ico)$">
FileETag None
Header unset ETag
Header set Cache-Control "max-age=2016000, public"
ExpiresByType image/x-icon "access plus 1 year"
</FilesMatch>
</IfModule>
<IfModule mod_expire.c>
ExpiresDefault "access plus 7 days"
ExpiresActive On
# CSS and JS are often not modified but may require an expiration date
ExpireByType text/css "access plus 1 year"
ExpireByType application/javascript "access plus 1 year"
</IfModule>
<IfModule mod_mime.c>
AddType application/x-php .php
AddType application/x-httpd-php .phtml
</IfModule>
-
Save the file and close it.
-
Restart your Xampp server to apply the changes.
The Rationale Behind the Solution
The added lines under <IfModule mod_mime.c> specify that files with extensions .php and .phtml should have the MIME type application/x-php, which is recognized as executable by Magento’s strict security settings.
This solution ensures you maintain a secure environment while resolving the error. It doesn’t disable any critical checks but simply updates the configuration to match Magento’s requirements for PHP execution.
Conclusion
In this post, we explored the “MIME Type Not Executable” error in Magento2 and provided a correct solution that maintains security standards. By updating the .htaccess file with the appropriate MIME types for PHP files, you can resolve this issue without compromising your site’s integrity.
Remember to always backup critical files before making configuration changes, and test thoroughly on staging environments before deploying updates to production.
For more comprehensive guides on Magento development and best practices, feel free to explore our resources at Laravel Company.
Happy coding!