1

I moved my site based on Magento 2 from hosting to my localhost. I cleared cache, adjusted(secure and unsecure) URLs in core_config, run static content deploy() using CLI. Checked all permissions for "folder".

Magento runs but with no CSS and js files.

In console I can see the following:

Printscreen of main Magento 2 page after deploying

What should I do to remove this issue?

P.S

  • Win 10
  • Open Sever (PHP7x64, MySQL5,7x64, Apache-PHP7-x64+Nginx1.10)
  • No external caching

P.P.S Before I copied the site from the host I tried to setup Magento with sample data using CLI and I received the same issue! So I believe it's not the only issue about moving Magento 2 from host to local. I can see that M2 tries to load all files from the version1485628564 folder which doesn't exist in the pub/static

http://magehost.two/pub/static/**version1485628564**/frontend/Magento/luma/en_US/mage/calendar.css

6 Answers 6

5

You need to update the .htaccess file under /pub/static folder. Open MAGENTO_DIR/pub/static/.htaccess and add the following code:

...
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /pub/static/ # <- Add This
...

Alternatively, you can disable static file signing by adding this record into the core_config_data table with this query:

INSERT INTO `core_config_data` VALUES (NULL, 'default', 0, 'dev/static/sign', 0);

In this case, keep in mind that this will disable the browser's cache refreshing mechanism. After the execution, you have to flush the Magento cache.

UPDATE 2018

In 2018 I've made a Pull Request to the Magento 2 team that includes this fix. Latest versions of branch 2.3 and 2.4 include the above row in the .htaccess file:

## you can put here your pub/static folder path relative to webroot
#RewriteBase /magento/pub/static/

You have to uncomment the row and set it accordingly to your Magento installation.

You can find the same row under the /pub/media/.htaccess file.

3
  • Thanks, second way was successfull. Commented Apr 7, 2017 at 10:23
  • Disabling the dev/static/sign might resolve this problem quickly, but it also sacrifices the browser cache refreshing mechanism, which is a must have feature for a production website. From the description of this question, @StepanFurman mentioned that version1485628564 folder doesn't exist under the pub/static folder. This implies that he was not aware of the instructions in nginx.conf that removes this part from the final PHP request. So the @PixeMedia's answer following is the better answer.
    – hailong
    Commented Mar 8, 2019 at 2:57
  • Important note if you run your webroot from magento-root/pub/ make sure to remove the magento prefix and use: RewriteBase /pub/static/
    – Sjaak Wish
    Commented Jun 15, 2021 at 21:21
2

As you are using nginx, the htaccess comment above wont help you. You need to add this to your nginx domain config;

location /static/ {
# Remove version control string
location ~ ^/static/version {
  rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
}
1
  • I have the same problem . I read your solution ,but i am using windows10 , xampp . Where i have to edit this code ? . In my magento installation root directory there is one nginx.conf.sample file & in that file same code is there by default Commented Jul 17, 2019 at 10:06
1

It means your deployed_version.txt is removed. Add it again and deploy your Magento 2. Then it will work fine.

deployed_version.txt has to exist in pub/static/.

1
  • deployed_version.txt - is that added automatically during static deployment (as you can see in my question, I did it). Adding deployed_version.txt will not solve this. You also can see, that question is pretty old and already answered. And the right answer is already marked as answer. Thanks. Commented Aug 31, 2018 at 12:54
0

You need to run below command on CLI

  • path to Magento root folder : php bin/magento setup:static-content:deploy
  • path to Magento root folder : php bin/magento cache:flush
0
0

Add one more answer that might be helpful here. Firstly, if the website is set to production mode, make sure you run the command to deploy the static assets as below:

php bin/magento setup:static-content:deploy

Second, if your site is hosting with Nginx, make sure you include the nginx.conf.sample file located at the Magento 2 root folder. More specifically, following is the snippet (Magento 2.3.0) which handle the static assets requests:

location /static/ {
    # Uncomment the following line in production mode
    # expires max;

    # Remove signature of the static files that is used to overcome the browser cache
    location ~ ^/static/version {
        rewrite ^/static/(version[^/]+/)?(.*)$ /static/$2 last;
    }

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|json)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "SAMEORIGIN";
        expires +1y;

        if (!-f $request_filename) {
            rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
        }
    }
    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;

        if (!-f $request_filename) {
           rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
        }
    }
    if (!-f $request_filename) {
        rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}
0

You might want to check your Nginx configuration to ensure that it is allowing includes - that is what happened in my case. Without this setting, it will not look at your site nginx.conf file and the server will not be able to find your css, img or js files.

This link has instructions: https://www.inmotionhosting.com/support/edu/wordpress/advanced-nginx-vps-and-dedicated/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.