Contact Latest Articles Home rafled dot net

How to fix CORS issue when loading files from different domain


16th Jan, 2021 | Tech


For Apache2 Servers

Add this to .htaccess on the root directory of your domain. You may add other file extenstions as per your need.


<FilesMatch ".(eot|otf|ttf|woff|woff2|css|jpg|jpeg)">
    Header always set Access-Control-Allow-Origin "*"
</FilesMatch>

For nginx Servers

Add this to your vhosts file within server {} directive and restart nginx. You may add other file extensions too as per your needs.


location ~* \.(eot|otf|ttf|woff|woff2|css|jpg|jpeg)$ {
    add_header Access-Control-Allow-Origin *;
}

You may as well check your nginx file if it contains any errors using following command

rafled@Ubuntu:~$ sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

Then, restart nginx


rafled@Ubuntu:~$ sudo systemctl restart nginx