Increase Page Speed Using Htaccess File in WordPress

Development

The Complete Guide

Developers are always looking for ways to increase site speed. This post is focused on how to Increase Page Speed using Htaccess (WordPress). You might already know that there are a lot of ways to increase speed of WordPress using Htaccess code. In the following article, I will walk you through five best Htaccess tricks to optimize website speed.

Increase Page Speed Using Htaccess (WordPress)
Increase Page Speed Using Htaccess (WordPress)
It is always important to optimize website speed for smooth and enriching user experience. And search engines also love faster page speed WordPress sites.
According to a study by Google, websites loads under 1 sec are likely to be placed higher in search results.

Further, I am assuming that since you already know most of the Htaccess tricks to optimize website speed and you have already implemented those Htaccess tricks as well.

However, WordPress website speed optimization using htaccess tricks is not easy, so I would recommend you to review your implementation again.

And if you are a beginner, then you must check your website speed optimization again, because this post is especially relevant for you.

Let’s begin with prerequisites for best htaccess for WordPress website speed optimization.

Table of Contents
Prerequisites – Htaccess tricks to Optimize website speedHow to Increase Page Speed using HtaccessEnable Gzip and DEFLATE Compression to Speed up WordPress website using HtaccessEnable Keep Alive – Speed up WordPress using HtaccessEnable Browser Caching – Leverage Browser Caching via HtaccessAdd Expires headers to leverage browser caching via HtaccessDisable Image HotlinkingEnable mod_pagespeed – Increase Page Speed using HtaccessLightSpeed Server Cache – Enable itWrapping It Up

Prerequisites – Htaccess tricks to Optimize website speed

Before digging into the details, I would recommend you to take following prerequisites into consideration.

Create a backup of your WordPress Htaccess file, since changes may break your site. Because your hosting provider may be not supported few hacks ~ ‘things’.
Open your Htaccess file for editing. While I am guessing you know about Htaccess file and how to edit it.
Disable all the caching plugins you are using. And in addition to that Purge all the cache created by these plugins.
Refresh your website and check Whether it is still working. And if you found that your website is not working. Find a possible solution for that and rectify the error.
Now we are finally ready to edit our Htaccess file.
Finally, test your website on Pingdom or GTmatrix to get a baseline of page loading time.
How to Increase Page Speed using Htaccess
First of all, let’s start with a list of Htaccess tricks to increase page speed, which I am going to cover in this post. The list I have compiled covers the major recommendations by most of the web developers in the world to increase WordPress website speed using Htaccess.

Start Compression – Gzip and DEFLET
Enable Keep Alive
Leverage Browser Caching
Disable Image Hotlinking
Activate mod_pagespeed
Enable LightSpeed Server Cache
Enable Gzip and DEFLATE Compression to Speed up WordPress website using Htaccess
The first Htaccess trick on the list is Gzip compression.

Do you know that compression can reduce the size of HTML files, JS and CSS files by 60% to 80%.?

I am able to reduce page size by 82.2% by enabling Gzip and DEFLATE Compression on my website. Read my detailed guide on the same – how to enable Gzip and DEFLATE compression to increase page speed using Htaccess.

Further, Add the below code in your Htaccess file to Enable Gzip and DEFLATE Compression.

Gzip Compression on Apache
# TN START GZIP COMPRESSION
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
# TN END GZIP COMPRESSION
DEFLATE Compression on Apache
# TN START DEFLATE COMPRESSION
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE “application/atom+xml” \
“application/javascript” \
“application/json” \
“application/ld+json” \
“application/manifest+json” \
“application/rdf+xml” \
“application/rss+xml” \
“application/schema+json” \
“application/vnd.geo+json” \
“application/vnd.ms-fontobject” \
“application/x-font” \
“application/x-font-opentype” \
“application/x-font-otf” \
“application/x-font-truetype” \
“application/x-font-ttf” \
“application/x-javascript” \
“application/x-web-app-manifest+json” \
“application/xhtml+xml” \
“application/xml” \
“font/eot” \
“font/otf” \
“font/ttf” \
“font/opentype” \
“image/bmp” \
“image/svg+xml” \
“image/vnd.microsoft.icon” \
“image/x-icon” \
“text/cache-manifest” \
“text/css” \
“text/html” \
“text/javascript” \
“text/plain” \
“text/vcard” \
“text/vnd.rim.location.xloc” \
“text/vtt” \
“text/x-component” \
“text/x-cross-domain-policy” \
“text/xml”
</IfModule>
# END DEFLATE COMPRESSION
And if you are on the NGINX server, then you have to add the following Htaccess code in your “config” file in order to enable Gzip and DEFLATE compression.

Compression on NGINX server
Copy and paste the following Htaccess code in the configuration file of your web server.

gzip on;
gzip_comp_level 2;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_disable “MSIE [1-6].(?!.*SV1)”;
gzip_vary on;
Enable Keep Alive – Speed up WordPress using Htaccess
Enabling Keep Alive is another powerful Htaccess trick to speed up WordPress website. It enables your server and web browser to download resources on a single connection, hence it increases page speed. You can enable Keep Alive by adding ‘Connection: Keep-Alive’ HTTP header in your server.

Add the following Htaccess code to enable Keep Alive and increase page speed using Htaccess (WordPress).

# TN START ENABLE KEEP ALIVE
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
# TN END ENABLE KEEP ALIVE
Enable Keep Alive in Apache
Keep Alive comes auto-enabled on most of the modern Apache servers. However, you can manually enable Keep Alive on old Apache Servers. Read about the same here – How to enable Keep Alive on old Apache Servers.

Enable Keep Alive in NGINX
Since you are using NGINX server, you probably don’t need to worry about enabling Keep Alive on NGINX servers. Keep Alive is enabled by default on NGINX server.

Enable Browser Caching – Leverage Browser Caching via Htaccess
Leverage Browser Caching via Htaccess is one of the most recommended Htaccess tricks to speed up WordPress website by the developers.

Most of all optimization tools like GTmatrix and Google PageSpeed Insights recommends to Enable Browser Caching. Browser Caching enabled websites told web browsers to store/keep website resources like JS or CSS files for a specified period.

So that, web browsers do not need to download the same resources again. As a result, the website loads faster because the browser uses the already downloaded resources.

Learn more about the Leverage Browser Caching via Htaccess, I have written a detailed guide about browser caching and how to implement leverage browser caching no your WordPress website.

You can Enable Browser Caching by adding the following Htaccess code to increase page speed using Htaccess.

Add Expires headers to leverage browser caching via Htaccess
# TN – START EXPIRES CACHING #
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css “access 1 month”
ExpiresByType text/html “access 1 month”
ExpiresByType image/gif “access 1 year”
ExpiresByType image/png “access 1 year”
ExpiresByType image/jpg “access 1 year”
ExpiresByType image/jpeg “access 1 year”
ExpiresByType image/x-icon “access 1 year”
ExpiresByType image/svg+xml “access plus 1 month”
ExpiresByType audio/ogg “access plus 1 year”
ExpiresByType video/mp4 “access plus 1 year”
ExpiresByType video/ogg “access plus 1 year”
ExpiresByType video/webm “access plus 1 year”
ExpiresByType application/atom+xml “access plus 1 hour”
ExpiresByType application/rss+xml “access plus 1 hour”
ExpiresByType application/pdf “access 1 month”
ExpiresByType application/javascript “access 1 month”
ExpiresByType text/x-javascript “access 1 month”
ExpiresByType text/x-component “access plus 1 month”
ExpiresByType application/x-shockwave-flash “access 1 month”
ExpiresByType font/opentype “access plus 1 month”
ExpiresByType application/vnd.ms-fontobject “access plus 1 month”
ExpiresByType application/x-font-ttf “access plus 1 month”
ExpiresByType application/font-woff “access plus 1 month”
ExpiresByType application/font-woff2 “access plus 1 month”
ExpiresDefault “access 1 month”
</IfModule>
# TN – END EXPIRES CACHING #
Add Cache-Control Headers to Increase Page Speed using Htaccess (WordPress)
Furthermore, add Cache-Control headers after that

# TN – BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch “\.(ico|jpe?g|png|gif|swf)$”>
Header set Cache-Control “public”
</filesMatch>
<filesMatch “\.(css)$”>
Header set Cache-Control “public”
</filesMatch>
<filesMatch “\.(js)$”>
Header set Cache-Control “private”
</filesMatch>
<filesMatch “\.(x?html?|php)$”>
Header set Cache-Control “private, must-revalidate”
</filesMatch>
</ifModule>
# TN – END Cache-Control Headers
Disable Image Hotlinking
Another Htaccess trick on the list is related to your site images.

Did you ever notice that someone is using your images on their website by using your image link directly?

Hmmm, No? YES? Not Sure?

Anyway, it really does not matter, whether you have noticed or not. There are chances that spammers can use your image link on their websites. And because of this, every time when someone visits that webpage, images will be loaded from your server. This is known as Image Hotlinking.

How does this (Image Hotlinking) affect your server? Well, it will use your server resources and bandwidth, which leads towards the higher load on your server. And hence slow or poor performance.

You can disable Image Hotlinking and Speed up your WordPress website using Htaccess. Add the following code in Htaccess. And do not forget to replace sample.com with your own domain name.

# TN – DISABLE IMAGE HOTLINKIING START
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?sample.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
# TN – DISABLE IMAGE HOTLINKIING END
Enable mod_pagespeed – Increase Page Speed using Htaccess
Furthermore, this Htaccess trick to increase page speed and to optimize your website is especially relevant and works well with Apache servers.

Mod_pagespeed module was developed by Google to increase page speed of websites. Nowadays, few Hosting Provider like Godaddy, SiteGround and DreamHost etc. supports mod_pagespeed module by default. However, you can have installed the same by using SSH on your control panel.

Mod_pagespeed can be enabled by adding the following code to the Htaccess file.

# TN – ENABLE MOD PAGESPEED START
# COMBINE CSS, COMPRESS IMAGES, REMOVE HTML WHITE SPACE AND COMMENTS
<IfModule pagespeed_module>
ModPagespeed on
ModPagespeedEnableFilters rewrite_css,combine_css
ModPagespeedEnableFilters recompress_images
ModPagespeedEnableFilters convert_png_to_jpeg,convert_jpeg_to_webp
ModPagespeedEnableFilters collapse_whitespace,remove_comments
</IfModule>
# TN – ENABLE MOD PAGESPEED END
You can customize your mod_pagespeed module by using customization options and filters available here.

LightSpeed Server Cache – Enable it
In addition to that, if you are using LightSpeed Server to run your website like we used to use LightSpeed Server with A2Hosting. You should use the following Htaccess code to enable server level caching. This will increase your speed up your WordPress website significantly.

# TN – ENABLE LITESPEED CACHE START
<IfModule LiteSpeed>
CacheEnable public
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^GET|HEAD$
RewriteCond %{HTTP_HOST} ^sample.com|sample.net|sample.org [NC]
RewriteCond %{REQUEST_URI} !login|admin|register|post|cron
RewriteCond %{QUERY_STRING} !nocache
RewriteRule .* – [E=Cache-Control:max-age=300]
</IfModule>
# TN – ENABLE LITESPEED CACHE END
Don’t forget to change sample.com with your own domain name. If you don’t want to enable caching on multiple domains delete the hyphen (|) and other domains.
Wrapping It Up
Before concluding this post, I have a bonus tip for you.

You probably know that your website can be hacked by only revealing your server information. Hide Server Signature by adding this code to your Htaccess file. Read more about server signature here.

# START – TN Disable server signature #
ServerSignature Off
# END – TN Disable server signature #
As a result, after the implementation of these Htaccess tricks, you will notice a significant increase in page speed using Htaccess.

Finally, do check your website on Pingdom or GTmatrix and compare the results with baseline.

Does your website loads faster without using any caching plugin? I am sure your answer would be a big YES!.

Share the percentage (%) change in your website loading time in the comments section below, or fire away your queries. I would be happy to help.

Peace!

Conclusion

If you need help with this guide, feel free to reach out for a Free Consultation.

Our technological powers increase, but the side effects and potential hazards also escalate. – HOST SSH