2025-09-27 | 148 Print
PDF
I will be sharing how I implement an array of cache techniques that will help you optimize and configure your Apache server (most cPanel servers are Apache-based) to control how the website behaves on a per-directory basis. It contains a series of settings designed to improve a website's speed, security, and performance. You can deploy any of the cached techniques and observe how it improves your website's loading speed.
Here is a breakdown of what each section of the code means:
1.) EXPIRES CACHING
This section sets browser caching rules to make your website load faster for repeat visitors by storing certain files on their local device.
• ExpiresActive On: Turns on the module (mod_expires) that controls file expiration.
• ExpiresByType: Specifies the expiration time for different file types based on their MIME type. For example:
o image/jpeg “access 1 year”: Tells the browser to cache JPEG images for one year from the time they are first accessed.
o text/css “access 1 month”: Caches CSS files for one month.
o text/html “access 1 month”: Caches HTML files for one month.
• ExpiresDefault “access 1 month”: Sets a default cache time of one month for any file type not explicitly listed above.
2.) Cache-Control Headers
This section provides more specific and modern control over caching behavior using the Cache-Control header.
• Header set Cache-Control "public": Sets the public directive for files like .ico, .jpg, .png, .gif, and .swf. This allows both the user's browser and shared caches (like a CDN) to store the files.
• Header set Cache-Control "private": Sets the private directive for JavaScript (.js) files. This means only the user's browser can cache the file, not any shared caches. This is a common setting for files that might be user-specific or sensitive.
• Header set Cache-Control "private, must-revalidate": This is set for HTML and PHP files. The private directive is used, but the added must-revalidate directive forces the browser to check with the server to ensure it has the latest version before using its cached copy.
3.) GZIP COMPRESSION and DEFLATE COMPRESSION
These two sections enable file compression to reduce the size of files sent from the server to the browser, which speeds up load times.
• The code first attempts to use the older mod_gzip module, and if that isn't available, it falls back to the more modern mod_deflate.
• mod_gzip_on Yes: Activates Gzip compression.
• mod_gzip_item_include file: Specifies which file extensions (.html, .txt, .css, etc.) should be compressed.
• AddOutputFilterByType DEFLATE: A directive for the mod_deflate module that lists all file types that should be compressed, covering a wide range of web content.
4.) Minify css/js
This section contains a set of rewrite rules for a specific minification tool.
• RewriteEngine on: Activates the rewrite engine.
• RewriteRule ^([bfg]=.*) index.php?$1 [L,NE]: This rule rewrites URLs to serve minified and concatenated CSS and JavaScript files. For example, a request for /min/f=styles.css is internally rewritten to /min/?f=styles.css and handled by a PHP script.
• SetEnv no-gzip: Disables Gzip compression for the minification process itself, preventing issues where files are compressed multiple times.
5.) DISABLE IMAGE HOTLINKING
This security measure prevents other websites from directly linking to your images, which consumes your bandwidth.
• RewriteEngine on: Turns on the rewrite engine.
• RewriteCond %{HTTP_REFERER} !^$: The condition is not met if the request has no referrer (like direct access).
• RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?seowebanalyst.com [NC]: The condition is met only if the referrer is not your own domain (seowebanalyst.com).
• RewriteRule .(jpg|jpeg|png|gif)$ — [NC,F,L]: If the conditions above are met, the rule is applied. The [F] flag sends a 403 Forbidden response to prevent the image from loading, and the [L] flag means it's the last rule to be processed.
6.) ENABLE MOD PAGESPEED
This enables and configures Google's PageSpeed module to automatically optimize your website's resources.
• ModPageSpeed on: Activates the module.
• ModPagespeedMapOriginDomain: Maps your domain for PageSpeed to operate.
• ModPagespeedEnableFilters: Applies specific optimization filters, such as:
o rewrite_css,combine_css: Combines multiple CSS files into one and rewrites them for efficiency.
o recompress_images: Compresses images to reduce their file size.
o convert_png_to_jpeg,convert_jpeg_to_webp: Converts images to more efficient formats (e.g., WebP for supporting browsers).
o collapse_whitespace,remove_comments: Minifies HTML by removing unnecessary whitespace and comments.
7.) ENABLE LITESPEED CACHE
This section configures caching specifically for web servers running LiteSpeed.
• CacheEnable public: Enables the public cache for pages.
• RewriteEngine On: Activates the rewrite engine.
• RewriteCond and RewriteRule: These directives work together to set a cache lifetime (max-age=300) for specific pages, excluding certain sensitive or dynamic areas like login or admin.
8.) Header unset X-Powered-By
• Header unset X-Powered-By: This directive removes the X-Powered-By HTTP response header. This header can reveal information about the software running on the server (e.g., PHP version), which can be a security risk. Removing it is a security best practice.
Conclusion
To conclude on the use of all these caching techniques, they each serve their specific purpose in the web performance and optimization of my website, and also helped to reduce the load on my server with respect to reducing Cloudflare error codes like 522, webpage breaking etc. you may not need to deploy all this configuration in your Apache server and might need the assistance of your host provider to confirm the use of some of the configurations. Kindly drop your insight on any one of them you have used and how they have benefitted you in the comment section, and lastly, don't forget to subscribe to my RSS via the social plugins below.
I am a seo web analyst and have a love for anything online marketing. Have been able to perform researches using the built up internet marketing tool; seo web analyst as a case study and will be using the web marketing tool (platform).
How To Optimize Cache Performance via HTACCESS Apache Server
How To Fix GA4 Showing Wrong Domain Traffic
How To Reactivate Google Adsense Account
How Do You Write Pitch Deck That Wins Investors
Effective Lead Magnet Funnel Examples For Businesses
How To Promote FMCG Products Using Digital Marketing
The Main Objectives Of SEO in Digital Marketing
How Artificial Intelligence Is Transforming Digital Marketing
Google CEO Sundar Pichai: Search will profoundly change in 2025
3 Most Important Business Growth Strategies