How to Optimize Your WordPress Site Speed with .htaccess

wprocket

Here is some snippets to help the website to load faster. All these snippets is used for Speed Optimization via .htaccess. Code is explained below.

# Disable ETags
<IfModule mod_headers.c>
	Header unset ETag
    Header set Connection keep-alive
</IfModule>
FileETag None
 
############## CDN Fix #############
<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>
 
########### REDIRECT TRAFFIC TO HTTPS ############
# RewriteEngine On
# RewriteCond %{HTTPS} off
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 
############ SECURITY ###########
<FilesMatch "\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$">
Order allow,deny
Deny from all
</FilesMatch>
 
<Files robots.txt>
Allow from all
</Files>
 
############## CACHING-GZIP ############
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A2592000
 
<FilesMatch "\.(txt|xml|js)$">
ExpiresDefault A2592000
</FilesMatch>
 
<FilesMatch "\.(css)$">
ExpiresDefault A2592000
</FilesMatch>
 
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$">
ExpiresDefault A2592000
</FilesMatch>
 
<FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$">
ExpiresDefault A2592000
</FilesMatch>
</IfModule>
 
<IfModule mod_headers.c>
  <FilesMatch "\.(txt|xml|js)$">
   Header set Cache-Control "max-age=2592000"
  </FilesMatch>
 
  <FilesMatch "\.(css)$">
   Header set Cache-Control "max-age=2592000"
  </FilesMatch>
 
  <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$">
   Header set Cache-Control "max-age=2592000"
  </FilesMatch>
 
  <FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$">
   Header set Cache-Control "max-age=2592000"
  </FilesMatch>
</IfModule>
 
<IfModule mod_deflate.c>
    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
            RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
        </IfModule>
    </IfModule>
    <IfModule mod_filter.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-ttf" \
                                      "application/x-javascript" \
                                      "application/x-web-app-manifest+json" \
                                      "application/xhtml+xml" \
                                      "application/xml" \
                                      "font/eot" \
                                      "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>
    <IfModule mod_mime.c>
        AddEncoding gzip              svgz
    </IfModule>
 
</IfModule>

Explaination:

  • Disable Etags:

Etag is the mechanism by which a browser check a newer version of resousces from the server. So when we disable it via htaccess file. The browser then force to use the cache file.

####Disable ETags####
 <IfModule mod_headers.c> 
     Header unset ETag 
     Header set Connection keep-alive 
</IfModule>
  • Allow CDN resources

If we are using CDN for optimization the site performance, we may see warning “Cross-Origin Resources Found”. Because when CDN works on a site many resouces is served from the CDN instead of the actual server. We can add this below code to allow cross-origin access.

############## CDN Fix ############# 
<IfModule mod_headers.c> 
     <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$"> 
         Header set Access-Control-Allow-Origin "*" 
      </FilesMatch> 
</IfModule>
  • Redirect http to https via .htaccess

The below is redirecting http request to secured protocol https. To get this working you must need to install a SSl certificate to the website.

########### REDIRECT TRAFFIC TO HTTPS ############ 
# RewriteEngine On 
# RewriteCond %{HTTPS} off 
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  • Security from uploading dangerous file types

We can further to improve security by denying all the dangerous file uploads by this.

############ SECURITY ########### 
<FilesMatch "\.(md|exe|sh|bak|inc|pot|po|mo|log|sql)$"> 
  Order allow,deny Deny from all 
</FilesMatch>
  • Allow to read robot.txt from everywhere from anything for SEO

<Files robots.txt> 
 Allow from all 
</Files>
  • Gzip Compression and caching Expiration

############## CACHING-GZIP ############ 
<IfModule mod_expires.c> 
    ExpiresActive On ExpiresDefault A2592000 
    <FilesMatch "\.(txt|xml|js)$"> 
        ExpiresDefault A2592000 
    </FilesMatch> 
    <FilesMatch "\.(css)$"> 
        ExpiresDefault A2592000 
    </FilesMatch> 
    <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$"> 
        ExpiresDefault A2592000 
    </FilesMatch> 
    <FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$"> 
        ExpiresDefault A2592000 
    </FilesMatch> 
</IfModule>
<IfModule mod_headers.c> 
    <FilesMatch "\.(txt|xml|js)$"> 
        Header set Cache-Control "max-age=2592000" 
    </FilesMatch> 
    <FilesMatch "\.(css)$"> 
        Header set Cache-Control "max-age=2592000" 
    </FilesMatch> 
    <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$"> 
        Header set Cache-Control "max-age=2592000" 
    </FilesMatch> 
    <FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$"> H
        eader set Cache-Control "max-age=2592000" 
    </FilesMatch> 
</IfModule>
<IfModule mod_deflate.c>
	<IfModule mod_setenvif.c>
		<IfModule mod_headers.c> 
            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding 
        </IfModule>
	</IfModule>
	<IfModule mod_filter.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-ttf" \ 
            "application/x-javascript" \ 
            "application/x-web-app-manifest+json" \ 
            "application/xhtml+xml" \ 
            "application/xml" \ 
            "font/eot" \ 
            "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>
	<IfModule mod_mime.c> 
        AddEncoding gzip svgz 
    </IfModule>
</IfModule>

These are the code by which we can do website speed optimization via .htacess. If you know something new please share with the comments.

GenesisPro728x90

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest
Share on tumblr
Tumblr
Anwer Ashif

Anwer Ashif

Founder of RainaStudio. Help businesses and individuals to create and outstand their online presence. Our success rate is measurable. Our blog served all around the world and counting.

Leave a Reply

Your email address will not be published.

Want to speed up WordPress site?