Magento 2 can't find css and js files

2019-06-12 11:28发布

问题:

thanks in advance for help!

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 permitions for "folder".

Magento runs but with no css and js files.

In console I can see the following:

Please give some advice, what should I do to remove this issue? Thanks in advance!

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

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

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

回答1:

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 you have to flush the cache after executing the query.



回答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;
}


回答3:

it means your deployed_version.txt is removed kindly add it again and delopy your magento 2 it will working fine.

deployed_version.txt is exists in pub/static/



回答4:

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


回答5:

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";
}