Cookie-free subdomain for static content

WordPress 4 min read

Anyone who has visited even a handful of websites on the internet is familiar with cookies to some extent. An annoying pop-up menu that shoves itself in your face and says ‘PLEASE ACCEPT’ is hardly a pleasant experience, but it’s something every other website displays when you click on it.

So, what are cookies? What’s the point of them?

Let’s answer these questions first, before we get into the whole ‘how’ of it.

Cookies are essentially small pieces of data that a website stores in your browser so that it can recognise you should you visit it again. You have no doubt noticed that on some websites you revisit, your preferences or settings are exactly as you left them. Yes, that is what cookies are for.

It’s a bit like going to the supermarket for your monthly shop and ordering a whole load of things. The shop assistant gives you a little token and says that the next time you show them this token, they’ll fetch all your items for you without you having to tick them off a list.

And that’s exactly what you do. Instead of wandering from aisle to aisle, picking out items and filling your trolley, you simply tell the shop assistant, hand over your token and wait for them to bring your purchases to you.

A cookie is therefore something that the website sends to you – to your browser. This is how the website knows who you are and what you want.

You might be thinking that this is all rather brilliant. What’s this whole ‘cookie-free domain’ business? Well, folks, it really is good. But only if it’s used when absolutely necessary. If your browser stores unnecessary cookies, things start to get slower and slower.

Completely unnecessary cookies accumulate in the browser just like dust on a doorstep. The website has to deal with all of them before it loads. This, in turn, slows down the loading speed.

And this is where you don’t need cookies.

For dynamic content – that is, content that needs to be created and tailored specifically to your needs and preferences – there is no alternative to the use of cookies. For example, if you visit a blog and read a few posts, the next time you visit, it will show you the ‘Recently Viewed’ content. So this feature – where something is generated and modified specifically for you – is what we call dynamic content.

In contrast, static content is content that simply sits there on a website and does not change, regardless of who visits it. Take, for example, an ‘About Us’ page. This ‘About Us’ page remains exactly the same every time you open it, regardless of who visits it. If you visit it several times, you won’t get a message saying ‘You’re back again, high school student?’, because the page doesn’t know who you are or how many times you’ve been there before.

So, with a cookie-free domain, you can display static content, but not dynamic content. This is the crux of the discussion above. The usual way to create a domain that only displays cookie-free content is to create a subdomain. As the name suggests, a subdomain is part of a larger, actual domain name.

For example, if there is a particular website with the domain name ‘www.webshop.hu’, then a subdomain might be ‘blog.webshop.hu’.

The process for creating one varies depending on the control panel. We have previously produced a guide for the cPanel interface, which can be accessed by clicking here. Generally speaking, the root directory of the new subdomain you create should point to the ‘wp-content’ folder of your WordPress website.

Once the above step has been completed, you must also configure WordPress so that it knows it needs to serve static content via the new subdomain.

To do this, we simply need to edit the wp-config.php file on our WordPress website and add two new options to it:

define("WP_CONTENT_URL", "static.weboldalam.hu");
define("COOKIE_DOMAIN", "www.weboldalam.hu");

The value of WP_CONTENT_URL will be the new subdomain we created earlier specifically for static content, whilst the value of COOKIE_DOMAIN will be the address of our website.

Please note: for existing websites, the above change will not apply to content uploaded previously, only to newly uploaded content!

Back: WordPress articles (43)