Best way to setup a cookieless domain for WordPress site (2020 update)
Previously we have published an article about the process of setting up an cookie free domain or cookieless domain, where we have mentioned about creating a static version of your website.
If you have read the article already you must known about what is cookies and how it is useful for storing user informations into the browsers.
However, in the other hand cookies can hinder your webpage performance by not delivering static content to the visitors web browsers.
One example of cookieless domain decreasing page performance: detected by gtmetrix
When a browser makes request for static files on your page such as an image, CSS file etc there is no need for it to add cookies with it, which results in sending additional traffic to the server. And additional traffic means more waiting time for the response by the server.
You can try these steps if you think your server response time is not good.
So the advantage of setup a cookieless domain is that you can separate the content that doesn’t require cookies from the content that requires it.
Using cookieless domain will deduct unnecessary network traffic and eventually increase the server response time + the page performance.
How to setup a cookieless domain for WordPress site
There are couple of ways to set up a cookieless domain. You can read all the procedures in this page. However, in this article, I will mention how to set it up manually for WordPress website.
Before starting the process one thing you need to remember that if you set cookies on the top level domain ( yourdomain.com) all the subdomains will also include the cookies in them. Because, as it sets on the primary domain its childs are going to have the DNA for sure.
So in this case, you are recommended to use a separate domain to deliver static content. It’s a better idea if you register a new domain and configure the web server to serve static resources from the new address. You will completely prevent any cookies to be set in the new domain.
Instead, you can also set two different domains. One which includes cookies and one which doesn’t set cookies. In such a case, will set cookies on a www subdomain (www.yourdomain.com) and separately host your static files from another subdomain like (static.yourdomain.com). This static domain will no longer send cookies with your static files of a page.
Read the following procedures to setup a cookieless domain in WordPress.
1. Create a subdomain
Go to your cpanel and create a subdomain like static.yourdomain.com. From this subdomain you will serve or host all the static files.
Go to subdomains > make sure you are on the page ” create subdomain” > place “static” on the subdomain box > let domain box stay in default name > click on create.
2. Point subdomain to wp-content
Now point the newly created subdomain to the “/wp-content” directory of WordPress installation folder.
To do it again go to your subdomains and edit the settings by clicking on the edit icon next to your new static domain.
It will take you to the document root settings where you need to update the root address from “public_html/static” to “public_html/wp-content”.
3. Update your wp-config file
Go to the file manager using cpanel or ftp client and edit the wp-config file. Make sure the following code is available or added inside the file.
<?php
define(“WP_CONTENT_URL“,“https://static.yourwebsite.com“);
define(“COOKIE_DOMAIN“,“www.yourwebsite.com“);
?>
- Remember to correct your domain names accordingly.
4. Direct url to new subdomain
Before taking this step make sure you backup your database in awar to any kind of misconfiguration. Better to practice with demo site before applying in live the version. I do not take any responsibility for further loss of data or website down.
Now you need to run sql query on database table. Go to phpmyadmin > select your wp database > go to SQL tab and paste the following code in the command box.
UPDATE wp_posts SET post_content = REPLACE(post_content,’www.yourwebsite.com/wp-content/’,’static.yourwebsite.com/’)
This will make sure all the post urls are directed to the new subdomain.
- Again make sure that you have corrected the domain details inside the code.
Once you setup everything your cookie domain and static domain are ready for action. And the static domain will start delivering static resources without sending unnecessary cookies traffic.
I hope this guide will help you to establish your static site without any additional problems. Make sure you comment down below in case you need any more information.