How To Use Cookie-Free Domains for High-Speed Website Performance

How To Use Cookie-Free Domains for High-Speed Website Performance

Today we discuss How To Use Cookie-Free Domains for High-Speed Website Performance. Cookies are how websites remember Without them, they’ll forget about you as soon as a page finishes loading. This ability to remember allows you to log into a content management system or place products in an eCommerce shopping cart. This is what allows a dynamic CMS like WordPress to personalize web pages. But sites are not required to include a cookie with every file they serve. And unnecessary cookies increase latency and generate unnecessary network traffic.

Cookie-free domains allow you to separate content that doesn’t require a cookie from content that doesn’t, ensuring that your site serves cookies only when needed. In this context, we explain how to install and use a cookie-free domain. We use CMS site WordPress as an example, but a similar process works with other ( CMS ) content management systems and eCommerce stores.

An HTTP Cookie

An HTTP cookie is a small piece of data that a site sends to a web browser when it first loads a page. A cookie contains a unique identifier that the browser stores along with other information about the site. When the web browser loads another page, it sends the identifier with the HTTP request. This way, the website knows when a series of requests come from the same web browser.

When logging into a website, you authenticate with a username and password. If you enter the correct credentials. The site sets a cookie so you don’t have to re-enter your credentials each time you load a page. This process is called session management; A series of otherwise independent requests are linked to a session by cookies.

This ability to remember is also used in other ways. The Site may send unique personalized content to each visitor; An eCommerce store, for example, can display your recent orders because the cookie links data associated with your account in the site’s database to an HTTP request to order. In more controversial applications, advertisers use cookies to track you; They set a cookie, and advertising code across the web uses it to create a profile of the products you’re interested in.

Why Use Cookie-Free Domains?

The web requires cookies, but that doesn’t mean every HTTP request does. For example, when you open a page with embedded images, each triggers an HTTP request. But images don’t change depending on who loads them, so serving cookies is a waste of bandwidth. You may want to load different images for different users, but this is handled in the HTML of the web page. The page requires a cookie; Not the picture.

The same applies to JavaScript code and other static assets like CSS. A page can load dozens or even hundreds of static assets, all with useless cookies attached. Times & bandwidths consumed by unnecessary cookies soon add up.

Removing cookies from static resources may seem straightforward, but cookies are controlled at the domain level. You cannot serve resources with and without cookies from the same domain. Instead, you need a cookie-free domain for static assets only.

Another domain-related wrinkle to consider when setting up cookie-free domains is that subdomains inherit cookie settings from their top-level domains. If you set up cookies for “example.com”, they are also served on “www.example.com” and “blog.example.com”, etc. So, you cannot host your dynamic content on “example.com” and your static content on “static.example.com”.

There are two ways around this:

  1. Use another domain to host static content.
  2. Use a subdomain for both dynamic content and static content. A typical example is to use “www.example.com” for dynamic content & “static.example.com” for static content.

In our walk-through for setting up a cookie-free domain, we assume that you host your site on a “www” subdomain and want to host static files on a “static” subdomain.

How to Create a Cookie-Free Domain in cPanel?

Open cPanel & navigate to the Subdomains tool, which you will find in the Domains section of the main page.

Create a new subdomain of the top-level domain, which is connected to your WordPress website. If your WordPress website is hosted at www.example.com, create a subdomain “static.example.com” or similar.

In the Document Root field, enter the “wp-content” directory of your WordPress site. In this case, the site is hosted on “public_html” so we write “public_html/wp-content”.

cpanel-cookie-free-domain

 

Configure WordPress to use cookie-free domains

We now need to tell WordPress to serve static content from the new cookie-free domain. For completing this, we will add two new directives to the site’s “wp-config.php” file. You can access the file on the command line, but you can also edit WordPress configuration files in cPanel’s File Manager, which you can open from the File section of the main page menu or directly in the WordPress Toolkit.

Find “wp-config.php” and click Edit on the toolbar.

wp-config

Add the following lines to the file with the appropriate edits to insert your subdomains.

  • define(“WP_CONTENT_URL”, “static.example.com”);
  • define(“COOKIE_DOMAIN”, “www.example.com”);

Click to Save Changes.

In the end, we edit the website’s database to redirect existing posts to the new subdomain. Before you complete this step, back up the database with the WordPress toolkit or the manual method we explained here. You are about to irreversibly edit the database If you get it wrong, your site will no longer function properly.

Open PhpMySQL from the Databases section in the cPanel menu or access the database directly from your site’s Databases tab in the WordPress Toolkit.
Select your WordPress site’s database and then its _posts table.
Click the SQL tab.
Type the following SQL code in the text box. Please confirm that you edit the URLs to match your subdomains.:

  • UPDATE wp_posts SET post_content = REPLACE(post_content,’www.example.com/wp-content/’,’static.example.com/’).

sql-edit

That’s it! Static content will now be served from your new cookie-free subdomain, and cookies will continue to be done from the “www” subdomain domain.

Scroll to Top