Last updated on February 27, 2019 by J M Mubasshir Rahman

Setup a Cookieless domain for WordPress site

Setup a Cookieless domain for WordPress site

In speed optimization, Cookieless domain plays one of the vital roles for page speed. It is undoubtedly one of the most important warnings to fix. This warning shows in every speed test tools. Today we will see how can we fix this issue.

What is Cookie

At first let me tell you what is cookie. Cookies are little records which are stored on a client’s PC. They are intended to hold an unobtrusive measure of data to a specific customer and website can be accessed either by the web server or the customer PC.

How to setup a cookieless domain for WordPress site

There are four steps to setup a cookieless domain for a WordPress site. They are:

  • Create a subdomain
  • Point subdomain to wp-content
  • Update your wp-config file
  • Update existing post content

Create a subdomain

If you using Cpanel then the steps are below for subdomain creating:

  •  Log into the cPanel for your website. The address is usually www.website.com/cpanel, and the username and password would have been provided by your host when you first joined.
  • Under ‘Domains‘, click on the icon for ‘Subdomains’
Create subdomain
  • In the ‘Create a subdomain’ box enter static
  • Click on the ‘Create’ button and wait for confirmation that the subdomain has been created.

Point subdomain to wp-content

Now point the subdomain to the “/wp-content” directory of WordPress.

create subdomain
  • In the ‘Subdomains‘ field and in the ‘Document Root’field enter the path to the wp-content folder, for example: /public_html/wp-content and click Create.

Update your wp-config file

Now open your wp-config.php file through cpanel or ftp and add these lines:

<?php
define(“WP_CONTENT_URL“,“http://static.yourwebsite.com“);
define(“COOKIE_DOMAIN“,“www.yourwebsite.com“);
?>

Note: Add your domain names accordingly.

Update existing post content

Before starting this step at first backup your DB. Now go to your Cpanel and follow these steps:

  • Under ‘Databases’, click on the icon for ‘phpMyAdmin’
  • Expand the database for the WordPress installation and click on the posts table
Cookieless domain for WordPress site
  • Click on the ‘Inline’ link, then in the text box enter the following command
UPDATE wp_posts SET post_content = REPLACE(post_content,
 'http://www.domainname.com/wp-content/uploads/','http://static.domainname.com/uploads/')
WordPress StaticContent6
  • Click ‘Go’  And it will solve your Use cookie-free domains warning.

Additional step if your theme uses font awesome icons

If you use font awsome icons you also need to do this step for Cookieless domain. For this:

  • Open your .htaccess and add these lines:
## EXPIRES CACHING ##

# ------------------------------------------------------------------------------
# | CORS-enabled images |
# ------------------------------------------------------------------------------
    
        <FilesMatch "\.(cur|gif|ico|jpe?g|png|svgz?|webp)$">
            SetEnvIf Origin ":" IS_CORS
            Header set Access-Control-Allow-Origin "http://www.domainname.com" env=IS_CORS
        </FilesMatch>
    
# ------------------------------------------------------------------------------
# | Web fonts access |
# ------------------------------------------------------------------------------

# Allow access from all domains for web fonts

    <FilesMatch "\.(eot|otf|ttc|ttf|woff)$">
        Header set Access-Control-Allow-Origin "http://www.domainname.com"
    </FilesMatch>

If you do these steps successfully it will pass your Use cookie-free domains warning.


If you enjoyed this tutorial, then you’ll love our support.  If you need anything relates to WordPress Speed Optimization please send us a message.

Keep Learning