Increase your website speed using .htaccess rules

Many people believe that they should have a fast website right ? It’s very important to speed up your website so that you don’t loose the users.Because a fast website is definitely a necessity in today’s world, the successful websites go well beyond a fast user experience . There are a few basic optimization tips that everyone can benefit from, such as CSS and JavaScript compression, HTML minification and CSS sprites, but do you know that there are also some server side settings that you can gives you to get a little speed boost . With the help of .htaccess file, it allows you to introduce new rules for basic site optimization such as mod_gzip , deflate, Expires headers etc. So let’s look at some rules to speed up your website using this .htaccessfile .

What is .htaccess ?

.htaccess file is standard text file and it is a special configuration file for use on web servers running the Apache Web Server software. htaccess files lets you control the behavior of your site or a specific directory on your site . And these files are only works on Linux servers. Using .htaccess file we may modify the configuration of the server software to stop or start running some server functionality and features.

Using an .htaccess file, you can:

  • Protect your site with a password.
  • Enable server-side includes.
  • Deny access to your site based on IP.
  • Change your default directory page (index.html).
  • Redirect visitors to another page.
  • Prevent directory listing.
  • Add MIME types.

As we mentioned above .htaccess files are a simple ASCII text file with the name .htaccess. This is not an extension like .html or .txt. The entire file name is .htaccess.

1 . deflating
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/
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
2 . Fonts

If you’re using custom fonts in your CSS, and you’re hosting them locally on your web space, then you can also deflate these

#compress fonts
Addtype font/opentype .otf
Addtype font/truetype .ttf
AddOutputFilterByType DEFLATE font/opentype font/truetype
AddType image/svg+xml .svg
AddOutputFilterByType DEFLATE image/svg+xml
3 . 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/ "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/ "access plus 1 month"
4. Cache Control

Browser cache control is directly related with the header expires date and according to the Apache documentation provides directives to control and modify HTTP request and response headers. that can be merged, replaced or removed.

# 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"
# END Cache-Control Headers
5. Gzip Compression

By compressing this page with GZIP, 76.1% bandwidth was saved. well, and it’s too easy! Just copy and paste these lines into the end of your .htaccess file

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

If you want to check whether your site is Gzipped after these changes are applied you can use the checkgzipcompression .

6 . Force Trailing Slash
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

Force trailing slash is great for SEO. The following snippet will always add a trailing slash to your site urls.

If you need help on speeding up your WordPress website, you can get in touch using the contact form and I’ll try to help you.

Hit like or Share and Please leave anything that you can think of in the comments section. I’m hoping we can come up with a nice sized list to help out all the newcomers. And feel free to ask your queries. Thank you Peace 🙂 !!!


Hey there !! Iam Ajay Bharadwaj a part time blogger from India. Specialized in web designing, front-end web development and WordPress expert since 2012. am well passionate for sharing knowledge that makes an impact. I write articles about on tech ,customizing templates designs and a lot more on wordpress.

What Others Are Reading :

Leave a Reply