Introduction
How many times did you see a “serve static content from a cookieless domain” error while analyzing the speed of your site (PageSpeed Insights, Pingdom, GTmetrix, etc.)? Well, when you see this warning message, that means your server sets cookies that are generating unnecessary and unwanted traffic.
In order to speed up your site and eliminate unwanted traffic you need to serve all static content, such as Images, JavaScript and CSS files, through cookieless domain. In this short tutorial will show you how to Serve Static Content From a Cookieless Domain.
Fix “Serve Static Content From a Cookieless Domain” warning
There are several ways to remove this warning, some of them include using a CDN (completely prevents receiving set-cookie response header) or domain/subdomain to serve static content.
For cookieless domain purposes in this example, we’ll set up a sub-domain, since this is one of the most popular methods.
Step 1 – Set up a sub-domain
There are several ways to create a sub-domain:
- Through the control panel (cPanel, etc.);
- Without control panel (through Apache/Nginx).
In this example we aren’t going to deal with sub-domain creation. If you don’t know how to create sub-domains, there are bunch of tutorials over the Internet, just google it.
Let’s assume that your primary domain is www.your-domain.com
, just create sub-domain. for example static.your-domain.com
, which you are going to use to deliver static content.
Step 2 – Configure DNS settings
After we have successfully created a sub-domain, we need to make certain changes and add some DNS records for our sub-domain using CNAME record. Set up sub-domain’s DNS record as a CNAME pointing to your main domain. You can do that through cPanel or manually by editing your DNS zone file:
static.your-domain.com. IN CNAME your-domain.com.
Step 3 – Re-Configure Website
The most important part of the setting is done, but there is one more step left. To load the static content from the sub-domain instead of the main domain, we need to change references within CSS, JavaScript files and images. For example, modify <img> source from this:
<img class="feature-img" alt="Feature Image" src="/images/feature_img.jpg">
into this:
<img class="feature-img" alt="Feature Image" src="http://static.your-domain.com/images/feature_img.jpg">
To re-configure WordPress site, you’ll need point your sub-domain to your /wp-content
directory by adding following lines into your wp-config.php
file:
define("COOKIE_DOMAIN", "www.your-domain.com"); define("WP_CONTENT_URL", "http://static.your-domain.com/wp-content");
That’s it! You are effectively serving static content from a cookieless domain. You can check that for example on Pingdom:
Use CDN to serve static content from a cookieless domain
There is another way to fix this warning, of course. Using CDN (Content Delivery Network) will ignore cookies/strip cookies and prevent the client from receiving the Set-Cookie response header. This alternative can save your time, you can check those popular CDN providers: KeyCDN and MaxCDN.
Want to improve your website loading speed? Check here for some tips.