Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /home/xaluano/public_html/seodigger/wp-content/plugins/adsense-earnings.php on line 46
» Speed Up Sites with htaccess Caching - SEO must haveComplete SEO tools and tip trick colections for webmasters

Categories

Top Hosting list

Baggets

sponsor:

Speed Up Sites with htaccess Caching - SEO must have

This article shows 2 awesome ways to implement caching on your Site using .htaccess (httpd.conf) files on the Apache Web Server. Both methods are extremely simple to set up and will dramatically speed up your site.

The modules need to be built into Apache; although they are included in
the distribution, they are not turned on by default. To find out if the
modules are enabled in your server, find the httpd binary and run httpd
-l
; this should print a list of the available modules. The modules we’re
looking for are mod_expires and mod_headers.

  • If they aren’t available, and you have administrative access, you can
    recompile Apache to include them. This can be done either by uncommenting
    the appropriate lines in the Configuration file, or using the
    -enable-module=expires and -enable-module=headers
    arguments to configure (1.3 or greater). Consult the INSTALL file found
    with the Apache distribution.

Caching Concepts

By setting an expiry time for the files on your Web site, you can go even farther than merely relying on the conditional GET and the 304 response that a server sends when a file has not changed… You can prevent the contact with the server from happening at all by using the Expires header and the Cache-control header.

If a browser receives an image with the cache control headers that say the image can be considered fresh for 2 weeks, then for 2 weeks the image can be pulled directly from the browser’s (or proxy’s) cache on subsequent requests.This is noticeably faster than even a conditional GET and a 304 response from the server since there is no round trip. After two weeks, a conditional GET would be sent to the server to check the Last-Modified date, then again, no requests would be made for the duration of the specified freshness period.

So, briefly, these 2 mechanisms require no coding changes to your existing Web pages, and they works to avoid the unnecessary requests and connections to your Web server for files that do not need to be requested with each visit. Part of the rationale behind ETags is to provide sub-one second resolution in validating cached entities, where Last-Modified are limited to one second resolution. The ETag certainly has its uses, but it serves more as an alternative to the Last-Modified value rather than an expiry-based caching mechanism.

#=============================================================================#
#          TIME CHEAT SHEET
#=============================================================================#
#      300   5 M                 #    604800   1 W
#     2700  45 M                 #   1814400   3 W
#     3600   1 H                 #   2419200   1 M
#    54000  15 H                 #  14515200   6 M
#    86400   1 D                 #  26611200  11 M
#   518400   6 D                 #  29030400   1 Y (never expire)

The first solution is the Apache Module mod_expires 1.3|2.0|2.2

Summary

This module controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. The expiration date can set to be relative to either the time the source file was last modified, or to the time of the client access.

These HTTP headers are an instruction to the client about the document’s validity and persistence. If cached, the document may be fetched from the cache rather than from the source until this time has passed. After that, the cache copy is considered “expired” and invalid, and a new copy must be obtained from the source.

Examples using mod_expires

EX. 1

ExpiresActive On
ExpiresDefault A300
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A3600
ExpiresByType text/css A3600
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType text/plain A300
ExpiresByType application/x-shockwave-flash A604800
ExpiresByType video/x-flv A604800
ExpiresByType application/pdf A604800
ExpiresByType text/html A300

EX. 2

ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType application/pdf A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/plain A2592000

# Expires after 4.8 hours
ExpiresByType text/css A17200

EX. 3

ExpiresActive On
ExpiresDefault A86400
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/css A2592000
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType text/plain A604800
ExpiresByType application/x-shockwave-flash A604800
ExpiresByType video/x-flv A604800
ExpiresByType application/pdf A604800
ExpiresByType text/html A900

Headers Sent with+without using mod_expires

WithOUT Expires

===> index.html
1  HTTP/1.1 2 Date: Sat, 25 Feb 2006 21:00:39 GMT
4  X-Powered-By: PHP/4.4.2
5  Keep-Alive: timeout=5, max=5
6  Connection: Keep-Alive
7  Content-Type: text/html

===> robots.txt
1  HTTP/1.1 2 Date: Sat, 25 Feb 2006 21:00:40 GMT
4  Last-Modified: Wed, 22 Feb 2006 23:23:13 GMT
5  ETag: “129ba06″
6  Accept-Ranges: bytes
7  Content-Length: 31
10 Content-Type: text/plain; charset=iso-8859-1

===> favicon.ico
1  HTTP/1.1 2 Date: Sat, 25 Feb 2006 21:00:43 GMT
4  Last-Modified: Tue, 13 Dec 2005 12:20:26 GMT
5  ETag: “17c22bd”
6  Accept-Ranges: bytes
7  Content-Length: 894
10 Content-Type: image/x-icon

===> example.css
1  HTTP/1.1 2 Date: Sat, 25 Feb 2006 21:00:53 GMT
4  Last-Modified: Thu, 23 Feb 2006 02:55:10 GMT
5  ETag: “b57d48″
6  Accept-Ranges: bytes
7  Content-Length: 17547
10 Content-Type: text/css

===> script.js
1  HTTP/1.1 2 Date: Sat, 25 Feb 2006 21:00:54 GMT
4  Last-Modified: Wed, 22 Feb 2006 11:50:47 GMT
5  ETag: “1cb6dc7″
6  Accept-Ranges: bytes
7  Content-Length: 3898
10 Content-Type: application/x-javascript

===> btn-send.png
1  HTTP/1.1 2 Date: Sat, 25 Feb 2006 21:00:57 GMT
4  Last-Modified: Thu, 16 Feb 2006 12:07:03 GMT
5  ETag: “b57d55″
6  Accept-Ranges: bytes
7  Content-Length: 608
10 Content-Type: image/png

WITH Expires

===> robots.txt
1  HTTP/1.1 2 Date: Sat, 25 Feb 2006 21:00:40 GMT
4  Cache-Control max-age=2592000
5  Expires: Mon, 27 Mar 2006 20:59:12 GMT
6  Last-Modified: Wed, 22 Feb 2006 23:23:13 GMT
7  ETag: “129ba06″
8  Accept-Ranges: bytes
9  Content-Length: 31
12 Content-Type: text/plain; charset=iso-8859-1

===> favicon.ico
1  HTTP/1.1 2 Date: Sat, 25 Feb 2006 20:59:12 GMT
4  Cache-Control max-age=2592000
5  Expires: Mon, 27 Mar 2006 20:59:12 GMT
6  Last-Modified: Tue, 13 Dec 2005 12:20:26 GMT
7  ETag: “17c22bd”
8  Accept-Ranges: bytes
9  Content-Length: 894
11 Connection: Keep-Alive
12 Content-Type: image/x-icon

===> example.css
1  HTTP/1.1 2 Date: Sat, 25 Feb 2006 20:59:19 GMT
4  Cache-Control: max-age=17200
5  Expires: Sun, 26 Feb 2006 01:45:59 GMT
6  Last-Modified: Thu, 23 Feb 2006 02:55:10 GMT
7  ETag: “b57d48″
8  Accept-Ranges: bytes
9  Content-Length: 17547
11 Connection: Keep-Alive
12 Content-Type: text/css

===> script.js
1  HTTP/1.1 2 Date: Sat, 25 Feb 2006 20:59:28 GMT
4  Cache-Control max-age=2592000
5  Expires: Mon, 27 Mar 2006 20:59:28 GMT
6  Last-Modified: Wed, 22 Feb 2006 11:50:47 GMT
7  ETag: “1cb6dc7″
8  Accept-Ranges: bytes
9  Content-Length: 3898
11 Connection: Keep-Alive
12 Content-Type: application/x-javascript

===> btn-send.png
1  HTTP/1.1 2 Date: Sat, 25 Feb 2006 20:59:28 GMT
4  Cache-Control max-age=2592000
5  Expires: Mon, 27 Mar 2006 20:59:28 GMT
6  Last-Modified: Thu, 16 Feb 2006 12:07:03 GMT
7  ETag: “b57d55″
8  Accept-Ranges: bytes
9  Content-Length: 608
11 Connection: Keep-Alive
12 Content-Type: image/png

Header Comparison using the below .htaccess

AddHandler application/x-httpd-php .htm
Options +FollowSymLinks -Indexes -ExecCGI
DirectoryIndex index.htm
AddDefaultCharset ISO-8859-1
AddCharset ISO-8859-1 .css
DefaultLanguage en
ServerSignature Off

ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType application/pdf A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/plain A2592000
ExpiresByType text/css A10800

PDF WITHOUT

Last-Modified: Mon, 20 Feb 2006 05:52:15 GMT
ETag: “1aa1fe1-36fa1″
Accept-Ranges: bytes
Content-Length: 225185
Connection: close
Content-Type: application/pdf

WITH

Cache-Control: max-age=2592000
Expires: Tue, 28 Mar 2006 16:12:43 GMT
Last-Modified: Mon, 20 Feb 2006 05:52:15 GMT
ETag: “1aa1fe1″
Accept-Ranges: bytes
Content-Length: 225185
Connection: close
Content-Type: application/pdf
Content-Language: en

CSS WITHOUT

Last-Modified: Sun, 26 Feb 2006 15:41:13 GMT
ETag: “b57d48-45b4″
Accept-Ranges: bytes
Content-Length: 17844
Connection: close
Content-Type: text/css

WITH

Cache-Control: max-age=10800
Expires: Sun, 26 Feb 2006 19:21:45 GMT
Last-Modified: Sun, 26 Feb 2006 16:21:38 GMT
ETag: “b57d48″
Accept-Ranges: bytes
Content-Length: 17828
Connection: close
Content-Type: text/css; charset=iso-8859-1
Content-Language: en

JPEG WITHOUT

Last-Modified: Wed, 22 Feb 2006 12:16:56 GMT
ETag: “b57d54-45e7″
Accept-Ranges: bytes
Content-Length: 17895
Connection: close
Content-Type: image/jpeg

WITH

Cache-Control: max-age=2592000
Expires: Tue, 28 Mar 2006 16:23:52 GMT
Last-Modified: Wed, 22 Feb 2006 12:16:56 GMT
ETag: “b57d54″
Accept-Ranges: bytes
Content-Length: 17895
Connection: close
Content-Type: image/jpeg
Content-Language: en

JAVASCRIPT WITHOUT HTACCESS

Last-Modified: Sun, 26 Feb 2006 07:39:29 GMT
ETag: “1cb6dc7-f2e”
Accept-Ranges: bytes
Content-Length: 3886
Connection: close
Content-Type: application/x-javascript

WITH

Cache-Control: max-age=2592000
Expires: Tue, 28 Mar 2006 16:25:54 GMT
Last-Modified: Sun, 26 Feb 2006 07:39:29 GMT
ETag: “1cb6dc7″
Accept-Ranges: bytes
Content-Length: 3886
Connection: close
Content-Type: application/x-javascript
Content-Language: en

PNG WITHOUT

Last-Modified: Thu, 16 Feb 2006 14:37:34 GMT
ETag: “6db4e0-141″
Accept-Ranges: bytes
Content-Length: 321
Connection: close
Content-Type: image/png

WITH

Cache-Control: max-age=2592000
Expires: Tue, 28 Mar 2006 16:26:42 GMT
Last-Modified: Thu, 16 Feb 2006 14:37:34 GMT
ETag: “6db4e0″
Accept-Ranges: bytes
Content-Length: 321
Connection: close
Content-Type: image/png
Content-Language: en

The second solution is mod_headers 1.3|2.0|2.2

#=============================================================================#
#          MOD_HEADERS
#=============================================================================#
# YEAR
Header set Cache-Control “max-age=29030400″   # WEEK  Header set Cache-Control “max-age=604800″   # 45 MIN  Header set Cache-Control “max-age=2700″

You can use mod_expires to take care of expires and max-age, and use mod_headers to “manually” configure
the following:Cache-Control: no-store
This object may not be stored in any cache, even the requestor’s browser cache.Cache-Control: no-cache
This object may be held in any cache but it must be revalidated every time it is requested.
Cache-Control: private
This object can be stored in the requesting browser´s cache but not in a shared cache …

Cache-Control: must-revalidate
Tells caches that they must obey any freshness information you give them about an object. The HTTP allows caches to take liberties with the freshness of objects; by specifying this header, you’re telling the cache that you want it to strictly follow your rules.

Cache-Control: proxy-revalidate
Similar to must-revalidate, except that it only applies to proxy caches.

#=============================================================================#
#          MOD_HEADERS
#=============================================================================#
# Turn on Expires and set default expires to 3 days
ExpiresActive On
ExpiresDefault A259200

# Set up caching on media files for 1 month
ExpiresDefault A2419200 Header append Cache-Control “public”   # Set up 2 Hour caching on commonly updated files  ExpiresDefault A7200 Header append Cache-Control “private, must-revalidate”   # Force no caching for dynamic files  ExpiresDefault A0 Header set Cache-Control “no-store, no-cache, must-revalidate, max-age=0″ Header set Pragma “no-cache”

Another example using Headers

#=============================================================================#
#          MOD_HEADERS
#=============================================================================#
# 3 Month
Header set Cache-Control “max-age=7257600″   # 1 Week  Header set Cache-Control “max-age=604800″   # 10 Minutes  Header set Cache-Control “max-age=600″   # NONE  Header unset Cache-Control Header unset Expires Header unset Last-Modified FileETag None Header unset Pragma

NOTE: Using FilesMatch and Files in htaccess


Last 5 posts in Seo Articles

Posted: January 30th, 2007 under Seo Articles.
Comments: none

Add to:
| | FURL | +Google | RawSugar | Slashdot | Y! MyWeb

Leave a Reply

You must be logged in to post a comment.