7 Easy .htaccess Tricks for Improving Website Performance

admin | April 17, 2012 | 0 Comments

An .htaccess file lets you configure and customize your web server settings, even if you don’t own the whole server. With .htaccess files, you can make configuration changes on a per-directory basis (just stick the .htaccess file in a certain directory, and the configuration directives will apply to the entire directory and all of its subdirectories). Below are seven easy .htaccess tricks you can use to improve your website’s performance.

#1: Prevent Hotlinking

Hotlinking refers to using a link to an image from another website instead of saving the image on your own site. It’s a common practice that can really eat up your website’s bandwidth. The code below will redirect all hotlinked images to a specific image (defined on line 6).

RewriteEngine On

#Replace ?mysite\.com/ with your blog url

RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]

RewriteCond %{HTTP_REFERER} !^$

#Replace /images/nohotlink.jpg with your “don’t hotlink” image url

RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

#2: File Caching

File caching is one of the most effective ways to speed up your website because it eliminates unnecessary HTTP requests on subsequent page views. This code snippet will set your site up with .htaccess file caching.

# 1 YEAR

<FilesMatch “\.(ico|pdf|flv)$”>

Header set Cache-Control “max-age=29030400, public”

</FilesMatch>

# 1 WEEK

<FilesMatch “\.(jpg|jpeg|png|gif|swf)$”>

Header set Cache-Control “max-age=604800, public”

</FilesMatch>

# 2 DAYS

<FilesMatch “\.(xml|txt|css|js)$”>

Header set Cache-Control “max-age=172800, proxy-revalidate”

</FilesMatch>

# 1 MIN

<FilesMatch “\.(html|htm|php)$”>

Header set Cache-Control “max-age=60, private, proxy-revalidate”

</FilesMatch>

#3: Expires Headers

For visitors who come back to your site often, you can use this .htaccess snippet to tell their browsers to keep the static content stored locally for a certain period of time and reuse it. This reduces the number of HTTP requests that are made, which conserves bandwidth.

<IfModule mod_expires.c>

ExpiresActive On ExpiresByType text/html “access plus 1 days”

ExpiresByType image/gif “access plus 2 weeks”

ExpiresByType image/jpeg “access plus 2 weeks”

ExpiresByType image/jpg “access plus 2 weeks”

ExpiresByType image/png “access plus 2 weeks”

ExpiresByType image/x-icon “access plus 1 years”

ExpiresByType text/css “access plus 2 weeks”

ExpiresByType text/javascript “access plus 2 weeks”

ExpiresByType application/x-javascript “access plus 2 weeks”

ExpiresByType application/x-shockwave-flash “access plus 2 weeks”

</IfModule>

#4: Compression with gzip

Mod_gzip is a handy external extension module for Apache web servers that helps reduce the size of web pages served over HTTP. If you already have mod_gzip installed on your server, you need to add the following in an .htaccess file for your hosting account.

<IfModule mod_gzip.c>

mod_gzip_static_suffix .gz

AddEncoding gzip .gz

AddEncoding gzip .gzip

mod_gzip_on YES

mod_gzip_handle_methods GET

mod_gzip_temp_dir /tmp

mod_gzip_can_negotiate Yes

mod_gzip_dechunk Yes

mod_gzip_send_vary On

mod_gzip_update_static No

mod_gzip_keep_workfiles No

mod_gzip_minimum_file_size 250

mod_gzip_maximum_file_size 1048576

mod_gzip_maximum_inmem_size 60000

mod_gzip_min_http 1000

mod_gzip_item_exclude reqheader “User-agent: Mozilla/4.0[678]“

mod_gzip_item_include mime ^application/pdf$

mod_gzip_item_include mime ^image/

mod_gzip_item_include mime ^application/x-javascript$

mod_gzip_item_include file .js$

mod_gzip_item_include file .css$

mod_gzip_item_include mime ^text/.*

mod_gzip_item_include file .html$

mod_gzip_item_include file .pl$

mod_gzip_item_include file .cgi$

mod_gzip_item_include handler ^cgi-script$

mod_gzip_item_include mime ^httpd/unix-directory$

mod_gzip_item_include mime ^application/postscript$

</IfModule>

#5: Data Compression

This code will compress data on your web server, which decreases response times by reducing the size of the HTTP response.

# data compression

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/javascript

#6: Preserve Bandwidth

This code will help you preserve bandwidth by enabling PHP’s built in transparent zlib compression. This can cut your bandwidth usage in half!

# preserve bandwidth

<ifmodule mod_php4.c>

 php_value zlib.output_compression 16386

</ifmodule>

#7: Disable Entity Tags (etags)

Etags provide a way for validating entities that is more flexible than the last-modified date. The problem is that etags are usually are created in a way that makes them unique to a specific server hosting a site. This means the etags won’t match when a browser gets the original component from one server and later tries to validate that component on a different server. You can avoid this site-slowing scenario by disabling your etags with the code below.

# disable etags

FileETag none

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Category: Website Tips

About the Author ()

Leave a Reply