Have you ever checked the performance of your website with an online tool? I’m sure you’ve seen a cookie-free domain name error one before.
For example, tools like Gtmetrix, Pingdom, and etc. analyze your entire website and display a report, and tell you which parts are working fine and which parts need extra attention. Whilst using this tool, you may come across the Use cookie-free domain error.
HTTP cookies contain data sent to the user’s browser by web pages. While browsing the website, this data will be stored in the browser. On the next visit, the data will be sent to the server to remind the user of their information.
What is the cookie-free domain? How and why should we use the cookie-free domain in WordPress?
In this article, we are going to discuss how to use the cookie-free domain in WordPress.
What is Cookie-Free Domain?
Here is a brief explanation of what a cookie-free domain is before we move on to how to use one in WordPress. The cookie-free domain is a domain that does not ask browsers to store cookies from the website.
Usually, websites have static information, which may never change. Images, CSS files, JavaScript and etc.
Due to the fact that these files are usually the same and remain unchanged, a cookie does not need to be saved by the browser. Therefore, if you change the domain, you can lower the requests the user sends to the server. This is beneficial in many ways.
This speed and performance can be used elsewhere rather than wasted on storing unnecessary cookies on the user’s browser. What is the main reason behind this? Well, the main reason is to increase the website’s performance.
By lowering the cookie requests, your website will run smoothly and it can answer necessary requests faster.
How to Use Cookie-Free Domain in WordPress?
We now know what is a cookie-free domain and why you should use it. Generally, using a cookie-free domain has two advantages:
- Reduces network traffic.
- Reduces the time required to load static content.
Reduce Loading Time for Static Content
Now it’s time to learn how to use the cookie-free domain in WordPress. Configuring the cookie-free domain in WordPress may seem difficult. But it’s very easy if you follow the steps below.
Much like other tutorials on our website, we would introduce multiple ways to use the cookie-free domain in WordPress. For example, using a CDN, or a separate domain. In this article, we are going to discuss viable solutions.
Keep in mind, if your domain name configuration is set to use cookies, all of the subdomains will use the same configuration. Therefore, to use the cookie-free domain you will need to use a separate domain for your static cookies.
The question is, how to use the cookie-free domain in WordPress?
1. Create a subdomain. For example, static.yourwebsite.com and configure it to receive all the static data.
2. Redirect the created subdomain to wp-content in your WordPress hosting. If you use cPanel, follow the image below. Change Document Root to public_html/wp-content.
3. In your host, look for the wp-config.php file and add the following code to it, or if the code already exists, replace it with the following:
define("WP_CONTENT_URL", "http://static.yourwebsite.com");
define("COOKIE_DOMAIN", "www.yourwebsite.com");
4. Now you must redirect all the posts to the new subdomain. Simply, run the following command in the SQL Database:
UPDATE wp_posts SET post_content = REPLACE(post_content,'www.yourwebsite.com/wp-content/','static.yourwebsite.com/')
You only need to do that. In order to receive static cookies, you simply need to follow the steps mentioned above in order to configure the existing subdomain to do so.
The next method is by using NGINX. If you would like to receive static data on the server, the first is step is to provide a cookie-free domain.
The next step is to connect the new domain to the same server your main domain is.
The first domain is the main domain and the second domain is the static domain.
maindomain.com A 11.22.33.44
static.maindomain.com A 11.22.33.44
Now it’s time to modify the ngix.conf file. Add the following code to the ngix.conf file:
server {
listen ip:80;
server_name maindomain.com;
root /srv/http/nginx/ maindomain.com;
access_log logs/ maindomain.com.access.log;
location / {
index index.html;
charset utf-8;
}
}
server {
listen ip:80;
server_name static.maindomain.com;
root /srv/http/nginx/maindomain.com;
location / {
if ($request_filename ~ "\.(jpg|css|gif|png|swf|ico|mp3)$") {
break;
}
return 404;
}
}
The next step is to load the images through the static domain, static.maindomain.com. Simply, reference static data to the domain.
Before:
<img src="/images/testimage.png" />
After:
<img src="https:// static.maindomain.com/images/testimage.png" />
Now if there’s any request for the image, it will be requested from static.maindomain.com.
Use Cookie-Free with CDN
You can also use a CDN in WordPress to use cookie-free domains. Most CDN provides the option for you to ignore cookies if you want. The challenge is to choose the right CDN for your needs. Different CDNs have different features. Combining them all can’t be a viable solution.
We are not going to discuss which CDN can cause issues. We would like to introduce a useful and right CDN to you. Our recommendation is KeyCDN. To use this CDN on your website you can use its plugin.
Follow the steps below:
1. First, head over to the official KeyCDN website and create a new account. This website allows its users to use one month for free and if you are happy with the services, purchase the full package.
2. From the official WordPress website, download the CDN Enabler plugin. Install and activate it.
3. From KeyCDN Dashboard, create a new Pull – Zone.
4. Copy the address of the created zone and paste it in the WordPress plugin you installed.
Paste the copied URL here:
- Once everything is done, delete your website’s cache.
This is everything you should do. To make sure you have followed the steps correctly, use one of the website analyzers tools to check your website.
Gtmetrix is Still Showing the Cookie-Free error
Cookie-free errors are among the most commonly occurring errors in Gtmetrix. No matter what you do to fix it, they always come back. Why is this happening?
You will still see the error in the analyzer if Strip Cookies and Cache Cookies are enabled in KeyCDN. That’s called the false-positive rule.
As mentioned above, when you set cookies on your main domain, all subdomains will use cookies as well. Even the CDN URL will use cookies.
If you have the Strip Cookies option enabled, the YSlow error is wrong. In fact, YSlow doesn’t check if you have this option enabled or not, which is why you receive it.
If you use a cURL in Chrome’s Dev tool, you can ignore this message.
In addition, if you are using Cloudflare, you can’t reach 100 in YSlow that easily. Cloudflare uses _cfduid cookie for each request and due to security purposes, you can’t delete it. Therefore, you will receive a cookie-free domain error.
2 Comments Leave a Reply
Remember that the changes to wp-config.php must come before the line
/** Sets up WordPress vars and included files. */
require_once ABSPATH . ‘wp-settings.php’;
or they will not take effect
Yes that is right. Thanks