Whether you are a developer or an ordinary internet user, almost all of us loves faster websites. This post is focused on how to increase page speed using Htaccess (WordPress). There are lots of ways to Increase WordPress speed using Htaccess. In the following article, I will walk you through five Htaccess hacks to speed up WordPress site.

It is always important to optimize your web page speed for smooth and enriching user experience. And search engines also love faster page speed WordPress sites.

According to a study, websites loads under 1 sec are likely to be placed higher in search results.

Since you probably know most of the Htaccess hacks to speed up WordPress site. And if you are a beginner, I would recommend you to review that again. Because this post is especially relevant for them.

Let’s Begin… with prerequisites

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 to increase page speed using Htaccess hacks, which I am going to cover in this post. The list I have compiled are the major recommendations by most of all the web developers in the world to increase WordPress speed using Htaccess.

  1. Start Compression – Gzip and DEFLET
  2. Enable Keep Alive
  3. Leverage Browser Caching
  4. Disable Image Hotlinking
  5. Activate mod_pagespeed
  6. Enable LightSpeed Server Cache

Enable Gzip and DEFLATE Compression to Speed up WordPress using Htaccess

The first hack 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.

Add the below code in Htaccess filed 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
  • Compression on NGINX server

Copy and paste the following 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

Enabling Keep Alive is an another powerful hack. 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 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 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

This is one of the most recommended Htaccess hacks to speed up WordPress site 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 same resources again. As a result, the website loads faster because browser uses the already downloaded resources. Learn more about the same, I have written a detailed guide about browser caching and how you can leverage browser caching.

You can Enable Browser Caching by adding the following code to increase page speed using 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 application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresDefault "access 1 month"
</IfModule>
# TN - END EXPIRES CACHING #

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 hack on the list is related to your site images.

Did you ever notice that someone is using your images on their websites by using your image link directly? 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 web page, 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 site 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]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
# TN – DISABLE IMAGE HOTLINKIING END

Enable mod_pagespeed – Increase Page Speed using Htaccess

Furthermore, this hack to increase page speed using Htaccess 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 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 following code into 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 use LightSpeed Server with A2Hosting. You should use the following 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 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 in your Htaccess file. Read more about server signature here.

# START – TN Disable server signature #
ServerSignature Off
# END – TN Disable server signature #

As a result of applying these Htaccess hacks, you will notice a significant increase 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 % change in your website loading time in the comments section below, or fire away your queries in the comment section below. I would be happy to help.

Peace!