Loading static content from a cookieless subdomainPrevious article Next article
Minimize your HTTP requests generating your CMS Made Simple-powered website by loading static content from a cookieless subdomain.
I investigated and have setup this method using my own websites and at my web host using cPanel. It is of course also possible at other platforms but I did not test and describe it here...
In my tutorials I use "website.com" as the default domain name in text and code.
It seems obvious, but it does bear repeating! Change this to *your* domain name!
How to use
In cPanel, open in the "Domains" section the "Subdomains" button.
Here you create a new subdomain "static" of your main domain.
Important! Point this subdomain to the folder where your main website is stored!
At most websites this will be: public_html.
But if your website is stored in a subfolder you need to add something like public_html/subfolder depending your settings.
After clicking the "Create" button the subdomain will be installed at your server.
This subdomain is an alias of the main domain without changing DNS (a/cname) settings.
In the "Security" section of the main cPanel page, you open the "Let's Encrypt™ SSL" page.
Issue a new certificate for the subdomain "static.website.com" you just created.
Add to the bottom of your config.php file (don't forget the change the file permission to not-writable afterwards)
ini_set ( 'session.cookie_domain', 'website.com' );
// Point all uploads paths to the static domain
$config['uploads_url'] = 'https://static.website.com/uploads';
// Change path for templates and stylesheets
$config['public_cache_url'] = 'https://static.website.com/tmp/cache';
// I needed to set this line, it might be you don't need it...
$config['ssl_css_url'] = 'https://static.website.com/tmp/cache';
Whether you use Matomo (before Piwik) or Google Analytics you need to update the code to prevent setting cookies for all subdomains. By default the cookie will be installed for ".website.com" (note the dot!) or "*.website.com". It has to be changed, so it only sets cookies for the current domain "website.com".
4.1 Example Matomo (Piwik)
Remove "*." twice.
4.2 Example Google Analytics
Remove "." once.
The theme and content files of your website are (mostly) stored in the uploads and/or assets folders. In example https://www.website.com/uploads/images/foo.jpg. Change the paths to https://static.website.com/uploads/images/foo.jpg. No need to move the files!
Do you use the CGSmartImage module at your site? Simply change the paths by changing these settings in the "General" tab.
1. Image Url Prefix: https://static.website.com/uploads/_CGSmartImage
2. Does the URL above acount for the cache path? Yes
These are all the steps you need to do!
When I use PingDom speedtest I can see at my website 73% of the HTTP requests are now done from the cookieless domain!
The website has certainly become faster by doing this!!!
No comments yet...