WordPress Speed Optimization by 200% using .htaccess Command

1 Share = 1 Coffee to Writer

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.

Accessing .htaccess File for WordPress Speed Optimization

If you want to edit your Apache configuration with .htaccess file then you have to keep in mind that any mistake may take your website down or internal errors.

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
/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 to that Goto Settings and Tick Show Hidden Files (dotfiles)

wordpress speed optimization

Enabling GZIP using htaccess commands:

By enabling GZIP for your blog or website you may increase its loading time up to 30-50% faster. It is best to compress the HTML to reduce your website size in addition to page running times. This will help your website retain visitors and improve page opinions.

Setting GZIP using htaccess commands on Apache server: Most of us using apache server for their web hosting service which means you 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

Adding DEFLATE

The best way to enable compression is using mod_gzip or mod_deflate for Apache software. Both modules basically do the same thing, compress data, but mod_deflate is better supported and documented, also easier to configure. If mod_deflate doesn’t work correctly on your website or web server for some reason of course you can use mod_gzip. Both compressions methods can be enabled by adding a simple code in WordPress .htaccess file:

# 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

 URL Canonicalization Redirection:

In this case, you can redirect your non-www URL to www URL. It creates confusion between the search engine and your exact URL. It becomes hard to determine which one your correct URL need to be indexed by Google or other search engines. By adding few lines in your .htaccess file you can easily resolve this issue and can do WordPress Speed Optimization.

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]

Enable Browser Caching (Expiry Header)

I am not using browser caching as per person choice but you should enable Browser’s Cache for your WordPress Speed Optimization in order to your blog or website for your returning visitors because once you enable this option, Important data, files will be stores in visitor’s system cache for specific time period as defined in htaccess and if customer visit your website again then data will be loaded from their local machine instead of calling from the server again and again.

#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

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