How to Increase Page Speed using Htaccess (WordPress)

Updated on

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). 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.

Fastest WordPress Hosting Anywhere!

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.

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.

  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 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

<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.*
  • DEFLATE Compression on Apache

<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/" \
"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/" \
"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" \

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).

<ifModule mod_headers.c>
Header set Connection 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

<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/ "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"

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 "\.(css)$">
Header set Cache-Control "public"
<filesMatch "\.(js)$">
Header set Cache-Control "private"
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
# 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 with your own domain name.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]

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.

<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

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.

<IfModule LiteSpeed>
CacheEnable public
RewriteEngine On
RewriteCond %{HTTP_HOST} ^|| [NC]
RewriteCond %{REQUEST_URI} !login|admin|register|post|cron
RewriteCond %{QUERY_STRING} !nocache
RewriteRule .* - [E=Cache-Control:max-age=300]

Don’t forget to change 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.


In addition to that, you may be interested to read the following tutorials –

Photo of author
Saurabh K
Saurabh K is a technology enthusiast and part-time blogger. He loves to explore the efficient use of technology and gadgets. He is an outlier and lensman. Add him in your social circle to know more.
Disclaimer: Affiliate links of some product(s) are being used on this page, if you follow the link and make a purchase, we may receive compensation from respective companies. This compensation comes at no additional cost to you.

32 thoughts on “How to Increase Page Speed using Htaccess (WordPress)”

  1. Avatar of Arvind

    nice post Saurabh….learned some new technique today of using htacess file ….would like to use these tips in future…thanks for sharing them…!!!

    • Hey Arvind,
      Thanks you for your feedback.
      Please do not use inappropriate links of your website in comments. We do not allow individual post links.

  2. Avatar of timo

    Hi, are these “hacks” also possible for non wordpress sites that have an htaccess file? For example, can I do this on a shared hosting plan from hostpapa? Or is this stricly for wordpress sites? Or are some things possible with only wordpress sites, while other “hacks” listed here are possible for any htaccess site?

    • It will work with any hosting or site where you are using htaccess file to control certain things.

  3. Avatar of Rene

    After following Shoutmeloud for some years! I must admit that you are my new hero! Thank you very much for your articles. My page speeds went up 30%.

    • Hey Rene, Glad, you find it useful. Thanks for stopping by.

  4. Avatar of AMAN KUMAR SINGH

    Great article and you described it very well. But one question how to optimize adsense ads for faster loading, cause adsense code badly affact my website speed

    • Try to Lazy load Ads. This is one of the most effective way to improve performance with Ads. Also, you can defer loading of Ad-JavaScript.

  5. Avatar of Dilip

    i make the changes and it seems my website speed goes from 27 to 89 and i feel very happy to see my site speed but after 24 hrs when i checked again my site speed goes to original level , after that again try to edit and replace the code but speed not increase , I not able to understood what is the issues and try a lot but not able to reduced my site size which showed 4.5 mb

    • You could have done many improvements on your website. Try to minify JS, CSS and HTML files, it will reduce no. requests. Compress your content using Gzip. And add expire headers for browser caching. This will definitely increase your website speed.

  6. Avatar of Nelson Opoku

    Hello Boss, what about the Mobile pagespeed does it also speed up my mobile site, or this hack is it just for only desktop

    • It will speed up your mobile site as well.

  7. Avatar of Mivel

    Hi Saurabh, great post, really enjoyed reading it. Truth be told, I’m somewhat new to all of this so I have some queries in mind. Can you tell me, since we’re not using any caching plugin, how do I clear cache when I want and can it be set to clear cache when I update/make new page/post? Thanks a lot in advance. 😉
    Now I’m going to set up a tent on your website and learn more from you.

    • Glad you find the article useful. There will not be any cache if you are not using any plugin. However, your hosting provider might have implemented server level cache. Generally, cache updated after editing of a page, otherwise you can trash the server level cache from your cPanel.

  8. Avatar of Richard Ndagijimana

    Hi Saurabh, this is a nice post man!! I have learned a lot on how to speed up my web apps. Thanks for sharing…!!!

  9. Avatar of Dennis Gaastra

    Hi Saurabh! Thanks for the nice article. We use Bitnami WordPress VMs where Pagespeed pre-installed and pre-activated. Some questions.

    [1] Can this information also be inserted with an Apache within a Virtual Host Configuration. Ideally, just calling a “Include technumero.conf”.
    [2] Could such a “technumero.conf” file be something maintained at Github? or does something like that already exist?
    [3] I am wondering why BitNami has mod_expires.c commented out by default? Would that be a mistake of Bitnami?
    [4] LiteSpeed does not come with Bitnami. Would it be worth installing?

    • Avatar of Saurabh K

      Hey Dennis, Glad you find this article useful.

      1. As you are using Bitnami WordPress VMs (on Google Cloud or AWS, just guessing), and because Bitnami modifies the default server configurations to make it more secure so these .htaccess hacks may not work with that setup.
      2. However, you can disable Pagespeed (make it on/off) by editing http.conf or pagespeed.conf file (/opt/bitnami/apache2/conf/).
      3. You add the caching code in htaccess.conf file to leverage browser caching. Find htaccess.conf file here /opt/bitnami/apps/wordpress/conf/htaccess.conf. Next use the Expired caching code in-between the directory tag.


      4. Don’t go for LiteSpeed, it won’t make much difference, if you have large amount of daynamic content.

      Hope you find it helpful, feel free to revert if you have any other query.

  10. Avatar of Onome Joseph

    Thank you so much Saurabh. I did not just speed up my site but learned a lot. Site speed can be such a pain, but thanks to you!

    • Avatar of Saurabh K

      Glad, I could help. Thanks for stopping by.

  11. Avatar of Nir

    Great advices: site started to fly.
    But Google page speed the same..

    • Avatar of Saurabh K

      Great to see that htaccess hacks are working. Try some mobile optimization hacks to improve Google PageSpeed score.

  12. Avatar of Preeti Rana

    It’s working really very well. Most people focus on keyword stuffing to rank on the top but you took care of content quality. Thanks for such a knowledgable blog. 😀

    • Avatar of Saurabh K

      Glad you find it informative. Thank you for stopping by.

  13. Avatar of André Silva

    Great improvements! But not getting how to reduce HTTP requests… i think it is the only thing that is slowing my site right now. Can you help me?

    • Avatar of Saurabh K

      Try to minify and combine CSS and JS files to reduce the HTTP requests, use any good plugin for the same. However, you can only combine files which are delivering from your server. For external files, you might need to consider other optimization techniques. Read this article for details.

  14. Avatar of Rae

    Great Piece of Content here Saurabh K, I wanted to know if there is a way one could achieve all this using a WordPress Plugin, not everyone is that technical to do all this through the HTACCESS, less we mess up things and the site even stops opening

  15. Avatar of PJ Digital Marketing

    Hi bro, Nice. I am using it for my website. Perfect codding, helped me to disable some of the cache plugins and using only this code.

  16. Avatar of ahmad baswedan

    very nice post Saurabh, however can i still modify Htaccess like
    you’ve suggested if i already intstall W3 Total Cache , thanks in advance

    • Avatar of Saurabh K

      Sure, you can. Just make sure you do not add the code for the same functions W3TC plugin is adding, like browser caching.

  17. Avatar of Omar Faruk

    Thank you but after adding this code without keep-alive my website image not showing. What is the reason behind this?

  18. Avatar of

    Very useful data, I was also looking for something that would speed up the website for me a bit. So how do we use it will also reduce the consumption of our pages because this part will already be in the browser’s memory?


Leave a Comment

Your email address and website details will not be published. Required fields are marked with *.