How to Fix 404 Error for Scripts and CSS in Magento 2

- Magento 2

Magento 2 Development; Magento 2 tutorial

If you’ve got Magento 2 from  GitHub or other sources; but after installation, 404 error occurs for scripts and CSS  from http://localhost/magento2/pub/static/, you’ve come to the right place. Below, you can find the possible solution to the problem.

First of all, check whether the Apache rewrite engine is on or not. If it is disabled, turn the engine on. If CSS and JS files are not loading, try to access them directly from the browser. The following exception is displayed?

Don’t panic! The solution to your problem is described below! The nature of Magento 2 forces it to create symlinks for some static resources when not in the production mode. You just need to change that behaviour. Follow the two simple steps described below:

  1. In app/etc/di.xml, find the virtualType name=”developerMaterialization” section and its item called name=”view_preprocessed”. Modify or delete it depending on your needs. To change the content of the item replace Symlink with Copy in Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink.
  2. To get rid of any existing symlinks, you need to delete the files. Go to pub/static and cleanse it but be careful: the .htaccess file is situated there as well – don’t delete it!

Now, the symlink error is solved. We hope this article was useful. It is based on the StackExchange question by piyush_systematix and answers by Chris O’Toole. For more useful articles, check out our Cookbook.