Home Website TutorialsWordPress How to fix the blocked by CORS policy error on WordPress

How to fix the blocked by CORS policy error on WordPress

by Thạch Phạm
Published: Last Updated on
A+A-
Reset

Fixing blocked by CORS policy error on WordPress with 2 simple steps.

Introduction

What is CORS?

CORS is a mechanism that allows many different resources (fonts, Javascript, icons, etc.) of a web page to be queried from other domains and displayed to the domain of that site. CORS stands for Cross-origin resource sharing.

How to recognize CORS policy error

When you notice that your website has some unusual signs such as font errors, interface errors, or icons on the website are displayed incorrectly, go to the website and press F12 >> and select the Console tab to view the log. If you see a log similar to the one below, your website has a CORS policy error. This error often occurs when using multiple domains for one website.

Access to font at 'https://abc.site/wp-content/themes/flatsome/assets/css/icons/fl-icons.woff2' from origin 'https://xyz.site' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

With the above log, you can understand that website xyz.site (sub) is calling API to website abc.site (main) to get icons resources, but the server of website abc.site does not have Header Access-Control-Allow-Origin or If its value is not valid, the error log will be returned as above.

The image I illustrated below is a typical example of a CORS policy error, the website cannot get icons, leading to display errors.

Fix the blocked by CORS policy error on WordPress

Implementation Guide

To solve this problem, we will have 2 methods as follows:

Method 1: Using the plugin

To handle CORS policy errors, you can install the HTTP Headers plugin and enable it.

Fix the blocked by CORS policy error on WordPress

Next, go to the plugin’s settings >> select ACCESS CONTROL

Fix the blocked by CORS policy error on WordPress

In this section, you choose Access-Control-Allow-Origin and click Edit to add a domain.

Fix the blocked by CORS policy error on WordPress

You choose On and add the primary domain and subdomain.

Fix the blocked by CORS policy error on WordPress

Now you will see the status On as shown below:

Fix the blocked by CORS policy error on WordPress

Method 2: Use .htaccess file

If you don’t want to install the plugin, you can also deal with the problem is to add the rule below directly to the .htaccess file of the site that has CORS policy errors.

# BEGIN CORS
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
# END CORS

The structure when added will be as follows:

Fix the blocked by CORS policy error on WordPress
Then you Save.

Below is the result after I have processed 1 of 2 methods above.

Fix the blocked by CORS policy error on WordPress

The above are very simple CORS policy error handling methods that AZDIGI would like to introduce to you, hope this article will be helpful to you. I wish you success in handling it if you encounter a similar situation.

You can refer to other user manuals at the link below

If you need support, you can contact support in the ways below:

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign