One 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:
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.