Support

Find answers, guides, and tutorials to supercharge your content delivery.

How to Use Cookie-Free Domains

Updated on April 14, 2023
How to Use Cookie-Free Domains

Are you looking to improve your website's performance and speed? One way to do this is by using cookie-free domains. In this blog post, we will explain what cookie-free domains are, why they are important, and how to use them to enhance your website's performance.

What are HTTP cookies?

HTTP cookies are small pieces of data that are sent from a website and stored in your browser. While a user is viewing a website that uses cookies, the cookies collect data pertaining to your website activity such as preferences, shopping cart items, which pages you visited, etc. Cookies are very valuable to many websites as they facilitate a better user experience and are key in performing certain functions such as determining whether or not a customer is logged in, and with which account.

Each subsequent time the user browses the same site, their browser sends the cookie to the website's server thus providing it with the information that was set in the last request. There are three main uses for cookies including:

  1. Session management: When a user logs in to a website, the website may use cookies to maintain the user's login status and keep them logged in as they navigate through the website. This is known as session management and is one of the most common uses of cookies.
  2. Personalization: Websites may use cookies to personalize the user's experience, such as remembering their language preference, displaying personalized content, or recommending products based on their browsing history.
  3. Tracking: Cookies can also be used to track user behavior across different websites and build a profile of their interests and preferences. This information can then be used to display targeted advertisements or personalize content.

There are also various types of cookies available and can each be used in different scenarios.

Although cookies are very useful in some cases, in other cases - such as the delivery of static content, they can hinder performance. When a browser makes a request for a static asset such as an image or CSS file, there is no need for it to also send a cookie to the server. This only creates additional network traffic, and since the files are static (they do not change), the server has no use for the added cookie.

However, there are several reasons why using cookie-free domains can be beneficial for your website:

  1. Faster loading times: By using a cookie-free domain for your static resources, you can reduce the amount of data that needs to be transferred between the user's computer and the server. This can result in faster loading times for your website, which can improve the user experience and reduce bounce rates.

  2. Better SEO: Website speed is a ranking factor for Google, and faster loading times can help improve your website's search engine optimization (SEO). By using cookie-free domains, you can optimize your website's performance and potentially improve its search engine rankings.

  3. Reduced server load: When a user requests a page on your website, their browser sends a request to the server for all of the resources needed to load the page, including cookies. By using a cookie-free domain, you can reduce the amount of data that needs to be transferred between the user's browser and the server, which can reduce the server load and improve your website's scalability.

  4. Improved user experience: Using cookie-free domains can improve website performance and speed, resulting in a better user experience. By serving static resources from a separate domain that does not set any cookies, the amount of data that needs to be transferred between the user's browser and the server is reduced. This can lead to faster loading times and a smoother browsing experience.

  5. Lower hosting costs: Hosting static resources on a separate domain can also help reduce hosting costs. This is because hosting providers often charge based on the amount of data that is transferred between the user's browser and the server. By reducing the amount of data that needs to be transferred, website owners can potentially reduce their hosting costs.

  6. Better prepared for a cookie-free future: As privacy requirements become increasingly strict, there is a growing controversy around the use of cookies. Some users are becoming more aware of how their personal data is being used and are choosing to block cookies or use privacy-focused browsers. By using cookie-free domains, website owners can be better prepared for a cookie-free future where users may have even stricter privacy requirements.

If you set your cookies on a top-level domain (e.g. yourwebsite.com) all of your subdomains (e.g. static.yourwebsite.com) will also include the cookies that are set. Therefore, in this case, it is required that you use a separate domain name to deliver your static content if you want to use cookie-free domains. However, if you set your cookies on a www subdomain such as www.yourwebsite.com, you can create another subdomain (e.g. static.yourwebsite.com) to host all of your static files which will no longer result in any cookies being sent.

The following steps outline how to use cookie-free domains in WordPress:

  1. Create a subdomain, such as static.yourwebsite.com, which is where you will deliver all your static files from.

  2. Point your new subdomain to the /wp-content directory of your WordPress installation. For cPanel users, you will need to update the document root field from public_html/static to public_html/wp-content like the screenshot below.

  3. Edit your wp-config.php file to reflect the following:

    define("WP_CONTENT_URL", "http://static.yourwebsite.com");
    define("COOKIE_DOMAIN", "www.yourwebsite.com");
    
  4. Run the following command in your SQL database, this will ensure all post URLs are directed to the new subdomain:

    UPDATE wp_posts SET post_content = REPLACE(post_content,'www.yourwebsite.com/wp-content/','static.yourwebsite.com/')
    

Now that your cookie domain and static content subdomain are set, you can begin delivering static content without the server setting an unnecessary cookie for static assets.

You can also use a CDN to deliver your static assets and use cookie-free domains while at the same time benefiting from the features a CDN has to offer. With KeyCDN, once you have setup a Pull Zone and retrieved the Zone URL or Zone Alias, you can simply integrate this into your chosen CMS or Framework.

KeyCDN provides the ability to ignore cookies through the Cache Cookies setting. The Strip Cookies settings prevents the client from receiving the Set-Cookie response header.

If both settings are enabled, your static content is being delivered via the closest CDN edge server and cookies are automatically stripped ensuring there is no additional cookie latency. This is an easy alternative from moving and configuring your site to deliver static assets from a separate subdomain. With a CDN that strips cookies, you can easily deliver content without cookies while taking advantage of all the other benefits that a CDN has to offer.

If you have the strip cookies and cache cookies options enabled, as shown above, however, when running your site through YSlow are still receiving a warning. This is due to a YSlow false-positive. As previously mentioned, if you set your cookies on the top-level domain (e.g., yourwebsite.com) all of your subdomains will also include the cookies that are set. This also includes your custom CDN URL if using one (e.g., cdn.yourwebsite.com).

However, as long as you have the Strip Cookies setting enabled in KeyCDN, even if you receive this warning it will be incorrect. YSlow does not take into consideration that the CDN actually strips the cookie and therefore may continue to throw the error. However, if you run a curl command on the asset or check it within the Chrome DevTools Network panel, you won't see any Set-Cookie headers. Therefore this YSlow warning can be safely ignored.

Additionally, if you are using Cloudflare then you simply won't be able to achieve 100 on YSlow. Cloudflare appends a __cfduid cookie to every request which cannot be removed due to security reasons.

Conclusion

Using cookie-free domains is a simple but effective way to improve your website's performance and speed. By serving static resources from a domain that does not set any cookies, you can reduce the amount of data that needs to be transferred between the user's browser and the server, resulting in faster loading times, better SEO, and reduced server load.

To implement cookie-free domains on your website, you need to choose a domain that does not set any cookies, set it up to serve static resources and modify your website code to use the new domain for all static resources.

Remember that website performance is crucial for user experience and can impact your search engine rankings. By optimizing your website's performance with techniques like using cookie-free domains, you can provide a better experience for your users and potentially improve your website's visibility and traffic.

Supercharge your content delivery 🚀

Try KeyCDN with a free 14 day trial, no credit card required.

Get started
KeyCDN uses cookies to make its website easier to use. Learn more