WordPress Speed Optimization by 200% using .htaccess Command

WordPress Speed Optimization by 200% using .htaccess Command

Not happy with your web hosting service provider?

TRY YOUSTABLE 75% OFF
WordPress Speed Optimization by 200% using .htaccess Command

WordPress Speed Optimization by 200% using .htaccess Command

1 Share = 1 Coffee to Writer

After you’re done putting on good quality content on your website and have made it eye-catching attractive, you have to know it is not everything. After SEO consideration and working on the website’s layout, your website should be fast enough for the users to actually visit once it ranks on search engines.

It doesn’t matter which kind of web hosting you are using Shared or VPS. In both servers, .htaccess commands have its own important contribution which can impact your WordPress Speed Optimization and many other things. Such as speeding up your blog, enable cache, redirection of the domain name and much more.

You’ve to strive to get your site loading in less than two seconds to impress the users and the algorithm Google uses. One of the ways to optimize your site is through .htaccess file.

What’s a .htaccess file?

It is a configuration file. .htaccess file is used Apache web servers found in the root directory.

What does .htaccess file do?

It is the key to control a particular folder or a piece of the server. .htaccess is very useful to edit the configuration of the server software resulting such as to start or pause some features or functionality of the server. To name a few, one can create diversions or password protected directories, block specific IPs, improve page loading speed and optimize the site.

Locating .htaccess file in WordPress

  • Assuming your WordPress site has generated a .htaccess file, it must be located in the root folder of the installation.
  • .htaccess file is a hidden file so make sure to ‘show hidden files’ when you’re using cPanel or FTP client.

This file can be accessed under this location easily and “username” will be your first 8 characters of your primary domain name.

WordPress Speed Optimization by 200% using .htaccess Command
/home/username/public_html

By default, .htaccess file is hidden due to some security reasons so you wouldn’t be able to see under public_html directory until you make it visible manually. For that:

  • Go to Settings and
  • Tick Show Hidden Files (dotfiles)
WordPress Speed Optimization by 200% using .htaccess Command

If You Can’t Find .htaccess file

Might be the reason, the concerned site has not generated a .htaccess file yet.

Set up permalinks to generate a .htaccess file:

  • Go to settings
  • Go to permalinks settings and click on save changes.

This should work. Alternatively, you can create a .htaccess file and upload it.

Before You Optimise your site Via .htacess

  • Stop the plugins creating any cache and get rid of the cache produced earlier by the plugins.
  • Check that your website is in a working state. Fix errors, if any.
  • Get an approximate idea of page loading time.

Speeding Up Your Page Using .htacess

Decreasing the page loading time is a multi-step process and there are a few features you need to enable or disable. After doing a little research, here is the list of most effective set of configurations most developers will recommend.

  • Enable Gzip and Deflate Compression
  • Enable Browser Caching
  • Enable Keep-Alive
  • Disable Image Hotlinking
  • Enable mod_pagespeed

Let us learn about all these steps one by one.

Gzip And Deflate Compression Speeding Up WordPress Site Using .htaccess

Compression merely reduces the size of the document, best recommended for XML files, JavaScripts, CSS stylesheets and also HTML files. Did you know that compression reduces the data to 60 to 80%?

The first thing is enabling compression, of course.

The two good ways to enable compression is through mod_gzip or mod_deflate if you’re using Apache[1]  Software. Mod_deflate is a little easier one, but there are chances your website or server doesn’t support it.

DEFLATE Compression

# BEGIN DEFLATE COMPRESSION
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
 AddOutputFilterByType DEFLATE application/javascript
 AddOutputFilterByType DEFLATE application/rss+xml
 AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
 AddOutputFilterByType DEFLATE application/x-font
 AddOutputFilterByType DEFLATE application/x-font-opentype
 AddOutputFilterByType DEFLATE application/x-font-otf
 AddOutputFilterByType DEFLATE application/x-font-truetype
 AddOutputFilterByType DEFLATE application/x-font-ttf
 AddOutputFilterByType DEFLATE application/x-javascript
 AddOutputFilterByType DEFLATE application/xhtml+xml
 AddOutputFilterByType DEFLATE application/xml
 AddOutputFilterByType DEFLATE font/opentype
 AddOutputFilterByType DEFLATE font/otf
 AddOutputFilterByType DEFLATE font/ttf
 AddOutputFilterByType DEFLATE image/svg+xml
 AddOutputFilterByType DEFLATE image/x-icon
 AddOutputFilterByType DEFLATE text/css
 AddOutputFilterByType DEFLATE text/html
 AddOutputFilterByType DEFLATE text/javascript
 AddOutputFilterByType DEFLATE text/plain
 AddOutputFilterByType DEFLATE text/xml
</IfModule>
# END DEFLATE COMPRESSION

If the concerned website isn’t supporting deflate, GZIP is your key.

Enabling GZIP using .htaccess commands

By enabling GZIP for your blog or website you may increase its loading time up to 30-50% faster This will help your website retain visitors and improve page opinions. Not only for a fast loading site, it’s always recommended to compress your HTML for the site.

Setting GZIP using htaccess commands on Apache server

Most of you using apache server for web hosting service can directly follow our instructions and turn on GZIP by adding a few deflate codes to your .htaccess file.

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

If You’re Not Using Apache Server

If you’re not using Apache server for web hosting, use the equivalents for the purpose   of compression.

Compression for NGINX Server

For the users stuck with NGINX server, fortunately ;), use this htaccess code in your configuration file in 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;

Enabling Browser Caching

Enabling browser caching means the browsers will download some resources in the local drive and when they are needed again, the browser uses those already stored files, consequently speeding up the process.

The files to be stored for how long is dictated by .htaccess.

Use these two codes methods to enable browser caching(recommended)

EXPIRES HEADERS

#BEGIN EXPIRES HEADERS
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default expiration: 1 hour after request
ExpiresDefault "now plus 1 hour"
# CSS and JS expiration: 1 week after request
ExpiresByType text/css "now plus 1 week"
ExpiresByType application/javascript "now plus 1 week"
ExpiresByType application/x-javascript "now plus 1 week"
# Image files expiration: 1 month after request
ExpiresByType image/bmp "now plus 1 month"
ExpiresByType image/gif "now plus 1 month"
ExpiresByType image/jpeg "now plus 1 month"
ExpiresByType image/jp2 "now plus 1 month"
ExpiresByType image/pipeg "now plus 1 month"
ExpiresByType image/png "now plus 1 month"
ExpiresByType image/svg+xml "now plus 1 month"
ExpiresByType image/tiff "now plus 1 month"
ExpiresByType image/vnd.microsoft.icon "now plus 1 month"
ExpiresByType image/x-icon "now plus 1 month"
ExpiresByType image/ico "now plus 1 month"
ExpiresByType image/icon "now plus 1 month"
ExpiresByType text/ico "now plus 1 month"
ExpiresByType application/ico "now plus 1 month"
# Webfonts
ExpiresByType font/truetype "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/x-font-woff "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
</IfModule>
#END EXPIRES HEADERS

Cache-Control

# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
 <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
 Header set Cache-Control "max-age=2592000, public"
 </filesMatch>
 <filesMatch "\.(css)$">
 Header set Cache-Control "max-age=604800, public"
 </filesMatch>
 <filesMatch "\.(js)$">
 Header set Cache-Control "max-age=216000, private"
 </filesMatch>
 <filesMatch "\.(x?html?|php)$">
 Header set Cache-Control "max-age=600, private, must-revalidate"
 </filesMatch>
</ifModule>
# END Cache-Control Headers
  • Enabling browser caching is guaranteed to speed up website loading.
  • If you’ve already enabled cache control by expires headers method and set the time limit you don’t need to do so, while enabling caching by cache control method in case you’re putting both of them in use. It’s not a problem to use both of them.

LightSpeed Server Cache

Also, if possible, try to enable LightSpeed Server Cache if you’re using LightSpeed Server for running your website.

Use this code:

# 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

Speeding Up WordPress by Enabling Keep Alive

Keep Alive is another trick that works; enabling it asks browsers to establish a TCP conection once and not multiple times. This kind of makes the browsers open a particularly HTTP connection persistently for all files loading on your web page for e.g. images, Javascripts, CSS scripts, etc.

Enable keep alive is always recommended since there are no downsides to it. To enable keep alive, you can do it with http header too. But we are going to it by .htaccess file here.

Enabling Keep Alive through .htaccess file

Edit the .htaccess file, add this code in it:

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

Keep alive is on by default on NGINX server and it mostly comes auto enabled on Apache servers.

Speeding Up Website by Disabling Image Hotlinking

This is really something you need to disable if you are not using image hotlinking feature because apart from reducing your website loading time this will save you from parasites. Many times, people can use the image on your website just by providing a link to it on their webpages. That way, every time the image is needed on any of the hotlinked pages, it is loaded from your server.

To disable image hotlinking use this code in your .htaccess file.

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

Enable mod_pagespeed for Faster Page Loading

Mod_pagespeed is an open source module for optimizing files like HTML, Javascript, PNG, JPEG and CSS automatically. There are more than 40 filters that compress and resize image, concatenate and minify Javascript and CSS and a lot more. Mod_speed kind of trims redundant content and minimizes HTTP requests.

Include this code in your .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
</IfModule>

After these guaranteed tricks to speed up your website on WordPress, let me tell you a bonus trick if you’re not aware of it already, might be useful sometime.

URL Canonicalization Redirection

In this case, you can redirect your non-www URL to www URL.

It becomes hard to determine which one of your correct URLs need to be indexed by Google or other search engines. By adding few lines in your .htaccess file you can easily resolve this issue which will optimize your site’s speed.

If you want to redirect www to non-www URL:

RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "http\:\/\/example\.com\/" [R=301,L]

If you want to redirect non-www to www URL:

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301

Libwww-Perl Access:

Check-in case your server makes it possible for access from User-agent Libwww-Perl. Botnet scripts which automatically seek out vulnerabilities in your software are sometimes identified since User-Agent Libwww-Perl. By preventing access from Libwww-Perl it is possible to eliminate quite a few simpler problems.

RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]

Now you’ve put all this effort on your website’s speed optimization, find the page loading time baseline again. I’m sure you’d be surprised.

2 thoughts on “WordPress Speed Optimization by 200% using .htaccess Command”

  1. 👇 YOU MIGHT LIKE THESE ARTICLES TOO 👇


Leave a Comment

Show Buttons
Hide Buttons