Magento2 404 Error – Fix for Scripts and CSS

404magento2xampp

I have downloaded and installed Magento 2 from https://github.com/magento/magento2

But I am getting 404 error for scripts and cs located in http://localhost/magento2/pub/static/

I have checked, Apache rewrite engine is on. But still, css and js file are not loading.

When I try to access a file directly from the browser it gives following exception.

Cannot create a symlink for "D:/xampp/htdocs/magento2/lib/web/mage/calendar.css" and place it to "D:/xampp/htdocs/magento2/pub/static/frontend/Magento/blank/en_US/mage/calendar.css" Warning!symlink(): Cannot create symlink, error code(1314)
#0 D:\xampp\htdocs\magento2\lib\internal\Magento\Framework\Filesystem\Directory\Write.php(155): Magento\Framework\Filesystem\Driver\File->symlink('D:/xampp/htdocs...', 'D:/xampp/htdocs...', Object(Magento\Framework\Filesystem\Driver\File))
#1 D:\xampp\htdocs\magento2\lib\internal\Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink.php(30): Magento\Framework\Filesystem\Directory\Write->createSymlink('lib/web/mage/ca...', 'frontend/Magent...', Object(Magento\Framework\Filesystem\Directory\Write))
#2 D:\xampp\htdocs\magento2\lib\internal\Magento\Framework\App\View\Asset\Publisher.php(65): Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink->publishFile(Object(Magento\Framework\Filesystem\Directory\Write), Object(Magento\Framework\Filesystem\Directory\Write), 'lib/web/mage/ca...', 'frontend/Magent...')
#3 D:\xampp\htdocs\magento2\lib\internal\Magento\Framework\App\View\Asset\Publisher.php(49): Magento\Framework\App\View\Asset\Publisher->publishAsset(Object(Magento\Framework\View\Asset\File))
#4 D:\xampp\htdocs\magento2\lib\internal\Magento\Framework\App\StaticResource.php(118): Magento\Framework\App\View\Asset\Publisher->publish(Object(Magento\Framework\View\Asset\File))
#5 D:\xampp\htdocs\magento2\lib\internal\Magento\Framework\App\Bootstrap.php(245): Magento\Framework\App\StaticResource->launch()
#6 D:\xampp\htdocs\magento2\pub\static.php(13): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\StaticResource))
#7 {main}

My operating system is Windows 7 and I am running xampp server.

Best Answer

When not in production mode Magento 2 will try to create symlinks for some static resources. You can change that behavior by doing the following.

  1. Open up app/etc/di.xml and find the virtualType name="developerMaterialization" section. In that section you'll find an item name="view_preprocessed" that needs to be modified or deleted. You can modify it by changing the contents from Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink to Magento\Framework\App\View\Asset\MaterializationStrategy\Copy

  2. Delete the files under pub/static to get rid of any existing symlinks. You may want to be careful not to delete the .htaccess file.

This should solve your error with the symlink.