WordPress Speed Optimization by 200% using .htaccess [No Plugin 2023]

After you’re done putting on good quality content on your website and have made it eye-catching and 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.

We recommend you use LiteSpeed-powered Shared or VPS hosting for better WordPress loading time. LSCache is 10X faster than any other cache available on WordPress Store.

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, enabling 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 the .htaccess file.

WordPress Speed Optimization Plugins

There are some plugins that can improve your WordPress website or blog loading speed but before you install any plugin you must understand what plugin will work for your depending on your Hosting Environment

LiteSpeed Plugin

LiteSpeed WordPress Plugin built to speed up your website loading speed hosted on LiteSpeed webserver however you can use this plugin if you are hosted on a standard Apache server but the cache wouldn’t be working perfectly.

LiteSpeed Plugin

It has an inbuilt Cache, CDN, and image optimization so you don’t really need to install multiple plugins for separate tasks. Most hosting providers offer LS Webserver hosting so because you need to install this plugin you should ask your hosting provider which web server they are using.

What’s a .htaccess file?

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

What does .htaccess file do?

It is the key to controlling a particular folder or a piece of the server. .htaccess is very useful to edit the configuration of the server software resulting such as starting or pausing 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 the first 8 characters of your primary domain name.

Locating .htaccess file in WordPress
/home/username/public_html

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

  • Go to Settings and
  • Tick Show Hidden Files (dotfiles)
Tick Show Hidden Files (dotfiles)

If You Can’t Find .htaccess file

This 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 from 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 the 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 are 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, but it’s also always recommended to compress your HTML for the site.

Setting GZIP using htaccess commands on the Apache server

Most of you using an 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 an 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 expiring the headers method and setting 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 connection once and not multiple times.

This kind of makes the browsers open a particular HTTP connection persistently for all files loading on your web page e.g. images, Javascript, 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 the 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 the 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 web pages. That way, every time the image is needed on any of the hot-linked 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 images, 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 the www URL.

It becomes hard to determine which one of your correct URLs needs to be indexed by Google or other search engines.

By adding a 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 a 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 in case your server makes it possible for access from User-agent Libwww-Perl.

Botnet scripts that 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 into your website’s speed optimization, find the page loading time baseline again. I’m sure you’d be surprised.

Facebook
Twitter
LinkedIn
WhatsApp

You May Also Like :

#15 Best Cheap Domain Name Registrars in 2023– Find Out Who’s #1

14 Best Free Anime Websites to Watch Anime Online 2023

Top 21 Sites like 123Movies and Best Alternative 2023

Top 7 Free Mathway Alternatives For Easy Mathematics 2023

On "WordPress Speed Optimization by 200% using .htaccess [No Plugin 2023]"

Average User Rating:
0
5
  1. https://www.lasendanatural.com

    Thanks for the info
    I have managed to improve my pagespeed score somewhat

    Reply
  2. Thank you! Copy and Paste works fine – no longer problem.
    I found this in an article for siteground services:

    ##Deny access to all CGI, Perl, Python and text files

    Deny from all

    ##If you are using a robots.txt file, please remove the
    # @TODO-KM sign from the following 3 lines to allow access only to the robots.txt file:
    #
    #Allow from all
    #
    #END Deny access to all CGI, Perl, Python and text files

    #SAY NO TO BAD BOTS DOING BAD THINGS
    SetEnvIfNoCase User-Agent libwww-perl bad_bots
    order deny,allow
    deny from env=bad_bots
    # END SAY NO TO BAD BOTS DOING BAD THINGS

    Reply
  3. this is useless without being able to copy the content

    can you remove this

    Reply
    • We have enabled to copy code

      Reply
      • No you have not enabled to copy code!

        Reply
  4. great article however it sucks not being able to copy the lines and implement them
    shame

    Reply
  5. Copying in this site is not allow!!!

    Reply
  6. hello, can you unblock the content of this page?? its been days now
    its useless atm

    Reply
  7. The author blocked the right mouse so you can’t copy it. Just useless

    Reply
    • We are removing the copy blocking script from this post so that you can copy the code

      Reply
  8. It’s not working for me 😢

    Reply
  9. nice article after this setting my website increase some traffic

    Reply
  10. Works very well:)

    Reply
  11. very helpful information for word-press developer

    Reply
    • Not only for developers, but newbies can also use these simple codes to speed up their WordPress blog or website 2X times better without paying anything

      Reply

Leave a Reply

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

Thanks for submitting your comment!

WordPress Speed Optimization by 200% using .htaccess [No Plugin 2023]

Wordpress speed optimization