Wordpress

Increase WordPress Page Speed 5x Using Htaccess Optimization

Many of us have suffered slow WordPress loading time due to lack of server-side optimization (Specially htaccess optimization) this happens mostly with new Webhosting services or cheap Webhosting packages. How do we get rid of it and speed up our WordPress speed by tweakings the htaccess file? A renowned Webhosting company like SiteGround Comes with Wordpress Optimization Especially in their Wordpress/WooCommerce Package Click Here to Check their Packages.

Why htaccess Optimization?

What if you are stuck with your current hosting and can’t move out or maybe you can’t afford some high-quality Webhosting services. That is why This post is focused on how to Increase Wordpress Speed using htaccess or simply htaccess optimization. 

It is always important to optimize website speed for a 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.

Before Begining the Optimization Process You might want to know what is a .htaccess file?

Increase WordPress Page Speed 5x Using Htaccess Optimization

.htaccess is a configuration file for use on Apache web servers located in the root directory. It gives the ability to control a specific folder or the piece of the server?Using .htaccess file we may modify the configuration of the server software to stop or start running some server functionality and features, for example (taking into account the purpose of this tutorial) we may:

  • Enable LightSpeed Server Cache
  • Leverage Browser Caching
  • Start Compression Gzip and DEFLATE
  • Activate mod_pagespeed
  • Enable Keep-Alive

Enable LightSpeed Server Cache

# 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>
# ENABLE LITESPEED CACHE END

Leverage Browser Caching :

1. Use Cache-Control Headers to Increase Page Speed using Htaccess (WordPress)

# 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>
# END Cache-Control Headers

2. Add Expires headers to leverage browser caching via Htaccess

# 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>
# END EXPIRES CACHING #

Enable Gzip Compression on Apache To Speed Up Page Load Speed

# 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>
# END GZIP COMPRESSION

DEFLATE Compression on Apache to Increase Wordpress Speed

# 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

Enable mod_pagespeed ? Increase Page Speed using Htaccess

# 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>
# ENABLE MOD PAGESPEED END

Enable Keep-Alive

# START ENABLE KEEP ALIVE
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
# END ENABLE KEEP ALIVE

Before Wrapping things up

WordPress or any other cms speed not only depends on the optimization from your end it also highly depends on server and hosting provider you use, even though my above optimization guide will help you speed things up but it’s highly recommended to use a better host like SiteGround. If you are serious about your online store with woocommerce or your business website you may consider using them.

Tags
Show More

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close
Close