Stepan Furman
Stepan Furman

Reputation: 158

Magento 2 can't find css and js files

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

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

Upvotes: 1

Views: 15243

Answers (6)

Muhammad Naseem
Muhammad Naseem

Reputation: 41

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/.

Upvotes: 1

Cristiano Casciotti
Cristiano Casciotti

Reputation: 1026

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.

Upvotes: 5

Jenna Brenn
Jenna Brenn

Reputation: 1

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/

Upvotes: 0

hailong
hailong

Reputation: 1515

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

Upvotes: 0

psh
psh

Reputation: 1

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

Upvotes: 0

PixieMedia
PixieMedia

Reputation: 1548

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

Upvotes: 2

Related Questions