How to create a cookieless domain on a WordPress website?
A cookie-free domain is important for improving website performance. Indeed, To improve the performance of the website, there is a recommendation from GTmetrix regarding the cookieless domain. What is this? How to implement it on WordPress? These questions will be the focus of this article. For example, we are going to show you an easy way to use it to speed up your website without using a plugin. Although the steps are easy to understand, you will need some basic PHP or copy-paste skills.
What is a cookieless domain?
Before talking about a cookieless domain, it is important to define what a cookie is.
What is a cookie ?
A cookie in web jargon is a piece of information that websites or applications save in your web browser. This is generally intended to facilitate the management of your information or preferences. For example, if you visit a car sales website. The website you visited may put data in your browser to subsequently make suggestions to you in relation to your preferences. This is quite common in the web world and several solutions would work with quite a limitation if there were no cookies. Cookies usually have a maximum size of 4 KB per domain name. It is important to note that as a user you can block or limit cookies on your web browser. As cookies accumulate in your browser, it affects how long you can take to navigate a website. This is the reason why you will notice that after a while a website takes longer to display information compared to your first visit.
What is a domain-less cookie?
The information relating to your photos or video is generally static (changes little). Therefore, having a domain name that does not retain cookies when serving images and other static files can help improve performance. A cookieless domain name is a domain name that you set up for your photos, videos, and other code like CSS and JS.
How can we implement a cookieless domain on WordPress?
To implement it on your WordPress website, you need to follow three steps:
- Create a subdomain for your website
- Make some adjustments in your wp-config.php code
- Update database
1- Create a subdomain for your website
If your domain name is domain.com, for example, you can create a subdomain called static.domain.com that will serve as your cookieless domain. The steps to follow may vary depending on your web server. Before creating a subdomain, you must make sure that your domain offers this possibility. Indeed, in your DNS record (the server where you manage your domain name settings), you must add a CNAME line similar to what you see below. It would therefore be important to make sure that your domain name is active after its creation before making the configurations. For this article, we will base our example on the cPanel. In your cPanel dashboard, follow these steps:
- Click on the subdomains icon
- Enter the subdomain name. For example, static; if you want static.domain.com as subdomain. Additionally, you need to make sure your subdomain points to your WordPress content path (wp-content).
2- Modify the wp-config.php file
Open the WordPress configuration file, located under the installation directory and paste the following code:
<?php /* Edit wp-config.php and add this code. replace prositeweb.ca by your own code */ define("WP_CONTENT_URL", "https://static.prositeweb.ca"); define("COOKIE_DOMAIN", "https://www.prositeweb.ca"); ?>
3. Update your SQL database with the following query
To update the database, you need to check your tables prefix in your wp-config.php file. Usually the prefix is wp_. However, it is possible to change when creating a website. Once you find the prefix. You will need to connect to your database server to make the request. This step involves changing the links to the images to point them to the new server. Below is an example request.
In the above query, you need to replace yourwebsite.com code > your domain name. Otherwise, if your database prefix is different from wp_, be sure to replace wp_posts code> with the correct prefix.
UPDATE wp_posts SET post_content = REPLACE(post_content,'www.yourwebsite.com/wp-content/','static.yourwebsite.com/');
Create a cookieless domain for a solution other than WordPress
The creation process could be similar as the case of WordPress. You would simply have to create the subdomain, and then replace the links of the photos and other static information to point them to the new domain. If you follow these steps, you will notice the performance improvement in your analysis report. If you followed this article correctly, you should have successfully created your cookieless domain on your WordPress site. Feel free to check out their website for different related content. If you need help optimizing the performance of your website, you can contact us through our contact form .