WordPress – How to serve static content from a cookieless domain

If you’re serious about optimizing a WordPress website for speed, then you need to setup your website to serve the static content, such as images and CSS files, from a cookieless domain. This will speed up your page loads by reducing unnecessary traffic and distributing the traffic across more two addresses instead of one, working around the limit that browsers place on how many simultaneous requests can be made on each address.

As described by Yahoo!, when the browser requests a static image and sends cookies with the request, the server ignores the cookies. These cookies are unnecessary network traffic.

The steps below detail how to configure your WordPress install to serve static content from a cookieless domain.

Step 1: Create a subdomain using cPanel for the static content

The steps below are for cPanel managed websites. The process may be different depending on the hosting environment for your website.

  1.  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.
  2. Under ‘Domains‘, click on the icon for ‘Subdomains’
  3. WordPress- cPanel Hosting
  4. In the ‘Create a subdomain’ box enter static in the ‘Subdomains field and in the ‘Document Root’ field enter the path to the wp-content folder, for example /public_html/wp-content
  5. subdomain
  6. Click on the ‘Create’ button and wait for confirmation that the subdomain has been created
  7. You will now be able to open static.domainname.com and see a blank page

Step 2: Configure WordPress

  1. Using your preferred method, for example FTP or through the cPanel, navigate to the root directory of your WordPress installation and edit wp-config.php
  2. Add the following lines to the top of the file, below the <?php
  3. Replace domainname with the domain name for your website
  4. define("WP_CONTENT_URL", "http://static.domainname.com");
    define("COOKIE_DOMAIN", "www.domainname.com");
    define("WP_PLUGIN_URL", "http://www.domainname.com/wp-content/plugins");
  5. Save the changes to wp-config.php

Step 3: Update existing post content

Now WordPress is configured to serve the static content in wp-content through the static subdomain.

New images added to posts will now automatically use the static subdomain, however existing image paths need to be updated to use the static domain.

  1. Log into the cPanel again
  2. Under ‘Databases’, click on the icon for ‘phpMyAdmin’
  3. Expand the database for the WordPress installation and click on the posts table
  4. Click on the ‘Inline’ link, then in the text box enter the following command
  5. Replace domainname with the domain name for your website
  6. UPDATE wp_posts SET post_content = REPLACE(post_content,
  7. Click ‘Go’ to apply the changes
  8. From the WordPress admin, check a few existing posts to make sure images are loading from static.domainname.com

And you’re done – one step closer to WordPress speed perfection!

OPTIONAL: Additional step if your theme uses font awesome icons

If your theme uses font awesome for icons, such as a ‘home’ or ‘search’ icon – you will need to add the following to your .htaccess file to allow the icons to continue to work.

Replace domainname with the domain name for your website


# ——————————————————————————
# | 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

# ——————————————————————————
# | 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”[:]

Related Post