4 Ways To Use Cookie-free Domains For Serving Static Contents

4 Ways To Use Cookie-free Domains For Serving Static Contents

  • 4
    Shares

We all do a website speed test to know our website loading speed and performance score. There are popular tools available like GTmetrix, Pingdom, Page-insight, Bitcatcha etc. These tools scan our website and point out which things are good and where changes are required.

Here is an example of a report provided by GTmetrix:

gtmetrix-speedtest-report-use-cookie-free-domains
gtmetrix-speed-test-report

As you can see, besides the image problem and loading time most of the test was passed by that specific page. To fix the image problem you can apply this trick.

Now if we go to the ‘Yslow‘ tab next to ‘page-speed‘, you may see a warning that says to use cookie-free domains. Check this image below.

use-cookie-free-domains-warning
use-cookie-free-domains-warning

If you are getting this problem for the first time then you should know what is a cookie-free domain? Why should you use cookie-free domains?

In case you already know about a cookie-free domain then skip to know “How to setup or use cookie-free domains on your WordPress site! ”

What are cookies?

An HTTP cookie is a small piece of data sent from a specific webpage or website. These tiny data are stored on the user’s computer by the user’s web browser when he is browsing.

What is a cookie-free domain?

A cookie-free domain is a very simple concept. It’s actually a domain which does not send a command to visitor’s browser to store cookies from the website.

Our website has some static contents such as image, javascript and CSS files. These contents don’t change so often. That’s why they don’t need to accompanied by cookies, as there is no user interaction with these resources.

So by configuring a domain that doesn’t set cookies, we can decrease the size of the total request made by visitor’s browser to our server, while trying to download page resources.

The concept behind this strategy is to reduce server latency by reducing the number of requests which can impact a little on your website performance.

You can watch the video to understand it visually.

As you have understood why you should use a cookieless domain, it’s time to know how do we set up one.

Setting up a cookie-free domain seems like a hard task but believe me it’s not. All you have to understand the process and do it perfectly.

There are a couple of ways to configure a cookie-free domain. Such as using a separate domain name,  set up a CDN and reconfiguring WordPress site. So we will discuss these methods one by and so on.

Here, we I will mention the tested methods which worked well for my blog.

1. Configure a cookie-free domain separately

In this technique, you will create a completely separate domain name for your website. This separate domain is going to host all the static contents without any cookie command to the browsers.

So, first of all, you have to register a domain name. If you already have then skip to the next step but if you don’t have yet buy a new one.

You can also create a sub-domain in case don’t want to buy a new one. If you prefer to use sub-domain, use a static prefix so people can understand by looking at the name. For example: “www.static.rankwordpress.in

*Remember: If you use your root domain without www version, like rankwordpress.in then using the subdomain as the cookieless domain will not work.

Again, if you are using Adsense advertisements then cookieless domain warning will always occur because your sub-domain is a part of your root domain and Adsense set cookies for every sub-domain under one verified domain name.

Read this tutorial to set up a cookie-free domain for both WordPress and non-WordPress website.

Steps:

After creating the cookieless domain, Use your .htaccess file to configure your web server to serve static contents without cookies. Here you have to put that .htacess  file into the root folder of your domain.

Here’s how to find and edit the .htaccess file

Once you find the .htaccess file paste the following code there.

 # Use Mod_deflate to compress static files
SetOutputFilter DEFLATE

# Speed up caching
FileETag MTime Size

# Expires
ExpiresActive On
ExpiresDefault "access plus 366 days"

# Future Expires Headers

Header set Expires "Sat, 27 Dec 2018 23:59:59 GMT"

Once you’ve set up the cookieless domain now you need to upload all of your static content to the new static sub-domain (or domain). These contents include CSS, ICOs, Javascript, text and image files.

This way you can create a cookieless domain from where static content of your original website will be served.

2. Use a CDN provider to ignore cookies

Using a CDN provider is another good way to solve the cookie-free domain problem. Most of the CDN has the ability to ignore cookies and strip cookies which helps to completely prevent the browsers from receiving the Cookie response header.

Since it’s hard to guess which CDN to use. Just because every CDN has different features and functions so not every CDN is suitable for you to solve the warning to use cookie-free domains.

If you are currently using Cloudflare CDN then you need to disable it to disappear the warning to use cookie-free domains. The reason is that you can’t disable cookies served through CloudFlare CDN. Moreover, they also include their security cookie in your website header.

But if you use CloudFlare with a caching plugin, you can change it to another CDN provider to solve the problem. KeyCDN is the nicest alternative I have ever used with my websites.

You can deploy keyCDN to your WordPress site by using a plugin. Just follow the steps below.

  • Go to their website KeyCDN and create an account there.  They charge about 0.04$/GB traffic data and you will get one month for a free trial for a new account. Test their service and if you feel better then go for a premium account. ( You don’t have to submit your credit details. )
  • Install the CDN enabler plugin from the WordPress repository.
  • Create a new Pull-Zone inside the keyCDN dashboard and save it.
create-pull-zone-for-cookie-free-domains
create-pull-zone-in-keycdn
  • Copy the CDN URL and paste inside your WordPress plugin.
copy-pull-zone-URL-cookie-free-domain
copy-pull-zone-URL
paste-url-inside-CDN-enabler
paste-URL-inside-CDN-enabler
  • Once your setup is done, clear your website cache and wait a little time to take the effect.

On completion of your set up, you should see the improvement in your website test results on gtmetrix.

3. Re-configure your WordPress site

In this method, you will set up your subdomain’s DNS record as a CNAME pointing to your root domain with the wp-config file.

To do that point your subdomain (any) to your /wp-content directory simply by editing your wp-config.php file.

Add the following codes into your wp-config file.

define("WP_CONTENT_URL", "http://static.domain.com"); 
define("COOKIE_DOMAIN", "domain.com");

* don’t forget to replace your domain URL and domain name inside the code. Example: http://static.google.com

4. Use Cookie-Free Domains – WordPress Plugin

One more alternate method is to use a WP plugin called WP2static. You can easily install the plugin from the WordPress repository.

wp2static-plugin
wp2static-plugin

After installing the plugin activate it. Now go to WP dashboard and select wp2static from the left bar menu.

Configure the settings inside the “Deploy static website” tab. See the image below.

use-cookie-free-domains-wp2static-configuration
wp2static-configuration

Once the export process is done, visit the URL that you have set during the configuration. (https://yourdomain.com/static).

Now check that URL in GTmetix. The warning to use cookie-free domains won’t be displayed inside the report.

Final Word

Using these cool tricks you can get around of that annoying cookieless domain warning on gtmetrix or Pingdom.

In the comment box please share is this article helpful for you or not. Also, do share with your friends if these tricks work well with you.



Don't miss out!
Subscribe To Newsletter

You will be notified with email when we publish editorial articles in this blog 

Invalid email address
Give it a try. You can unsubscribe at any time.

  • 4
    Shares

Rupam Krishna Bharali

Rupam is the founder of www.rankwordpress.in. He is a web developer, blogger, graphic designer and an MCA student from Dibrugarh University. He also loves traveling, exploring new ideas and connecting with people. Let's connect on: twitter

This Post Has 2 Comments

  1. Visitor Rating: 5 Stars

  2. Hello there I am so excited I found your webpage, I really found you by error, while I was browsing on Google for something else. Nonetheless, I am here now and would just like to say thank you for a marvelous post and an all round interesting blog (I also love the theme/design), I don’t have time to read through it all at the moment but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read much more, Please do keep up the great b.

    1. Thanks, Josefa, I appreciate your feedback. Keep visiting 🙂

Leave a Reply

Close Menu