WordPress and cookie free domains

wordpressOne of the tips you will get from Google’s Pagespeed Insights is to host your static content on a cookie free domain.

I always ignored this tip, but today I decided to find out how to accomplish this with WordPress. And it turned out to be relatively easy.

The first thing you have to do is create a new domain and let that point to the wp-content  directory of your website.

Let’s assume the domain of your website is yourdomain.com. You can choose to host your static content on a subdomain, for example static.yourdomain.com. The other option is to just use a completely different domain, for example static-yourdomain.com.

After you have your new domain pointing to the wp-content  directory of your website you have to add the following two lines of code to your wp-config.php  file:

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

And that’s it! You should now see all your assets served from your cookie free domain.

However, in my case I ran into some trouble when trying to log in again to the WordPress backend, somehow there was a redirect loop.

The fix was easy, I just removed the COOKIE_DOMAIN  constant from my wp-config.php  file and everything worked again.

There is one other thing you should be aware of if you are using a subdomain as your cookie free domain.

If there are cookies set for yourdomain.com your browser will also send them with every request for your subdomain, and you went to all the trouble of setting up a cookie free domain for nothing. So make sure there are no such cookies in your case!

However, if you force the www subdomain for your website there should not be a problem, because WordPress will set the domain of the cookies to www.yourdomain.com. But if your site can be reached on both www.yourdomain.com and yourdomain.com you should check this.

February 5, 2016

Click Here to Leave a Comment Below

Robert - March 6, 2018

Thank you for this article!
I would like to go a step further and move my css and js files to sperate subdomain. Bute I am a newbie and don´t know how to do it the right way.
I do have two other subdomains pointed to the content folder of the main domain.
Do I have to put this
define(“WP_CSS_URL”, “https://static-css.yourdomain.com”);
define(“WP_JS_URL”, “https://static-yourdomain.com”);
to my wp-config.php? Is the syntax right? Of course I will put my URL in.
Thank you for your help.
Robert

Reply
Leave a Reply: