Provide static content through Subdomain
Continue the website optimization program. In my previous article, I have instructions on how to do it Combine Subdomain and Cloudflare as a free image CDN.
With this new method, it will be simpler. Providing static content through Subdomain does not require using additional hosting to store images.
Create Subdomain
How to create a Subdomain is also quite simple, you just need to pay attention to the step of pointing the subdomain same directory with the main website.
Example of creating a subdomain in cPanel.
- Log in to cPanel and navigate to the section Domains → choose Create A New Domain.
- Enter the subdomain. For example
cdn.example.com
. - Check the box
Share document root (/home/username/public_html) with "example.com"
so that the subdomain can use the source of the main domain. - Press the button Submit.

NOTE: It is required to point the subdomain to the root directory of the main domain. Each management panel will have a different setup. Example of Domain Pointers Alias in DirecAdmin.
Add CORS header
Most popular browsers will ask you to set it up CORS header for your domain.
You can set CORS to file .htaccess
:
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
Replace URLs
This step simply rewrites the main domain name into a subdomain before serving it to users, so it will not affect the database. When you turn off the Plugin, the URL will return to the same.
There are many plugins that support this such as CDN Enabler, Litespeed Cache,...
If you use the CDN Enabler plugin you must press a button Save Changes
instead of
then it saves the CDN configuration.Save Changes and Validate Configuration

The setup in Litespeed Cache is similar to CDN Enabler but it has deeper options. You can configure different subdomains to serve different content.
For example:
images.example.com
give the image.statics.example.com
for css, js,... files

Connect Subdomain with Cloudflare
To optimize your subdomain, you need to connect to Cloudflare to take advantage of the speed of Cache CDN. You just need to enable proxy (Yellow Cloud) Just for subdomains. So this will not affect the main domain if you do not need to use it.
I wrote about the steps to connect and set up subdomains in Cloudflare in a previous tutorial here.
Related Articles