Make Your Website Fast by httaccess Expires Time

Unknown Reply 6:10 PM

3 Simple .Htaccess Rules For Better Website Performace

htaccess 3 Simple .htaccess Rules For Better Website Performace
.htaccess is the directory-level configuration file that allows us to adjust some sever configurations. Using .htaccess file effectively can increase website performance. In this article, we’ll see some simple rules for .htaccess file, that will make your website load faster!

Disable Etags

Entity tags (ETags) are a mechanism that web servers and browsers use to determine whether the component in the browser’s cache matches the one on the origin server. According to Yahoo! Performance Rules, disable ETags can make the page loads faster, decrease server load and reduce bandwidth if your website is hosted on multiple servers.
So, to disable ETags with .htaccess file, just put the following line into it:
  1. Header unset ETag
  2. FileETag None

Add Expires Headers

The expires headers tells the browser to store website’s components (images, scripts, flashs, etc.) in browser’s cache until they expire (defined by value of the expires header). Making far future expires headers that will make your page load faster because browser doesn’t have to request for those component, it simply takes them from the cache. Thus, this will decrease server load, too.
There’re several ways to define expires headers using .htaccess. You can set it with a specific time like this:
  1. Header set Expires "Tue, 16 Jun 2020 20:00:00 GMT"
If you want to add expires headers for some file types only, use the following code:
  1. <FilesMatch ".(ico|jpg|jpeg|png|gif|js|css|swf)$">
  2. Header set Expires "Tue, 16 Jun 2020 20:00:00 GMT"
  3. </FilesMatch>
The disvantage of this methos is that you have to specify the exact time of expires. There’s a second way that can be more flexible by using relative timestamp, based on current time of server or current time of access.
Try this line in your .htaccess file:
  1. ExpiresActive On
  2. ExpiresDefault "access plus 10 years"
This will add a very far future expires headers (10 years), based on time of access. If you want to use the current time, you can use:
  1. ExpiresActive On
  2. ExpiresDefault "now plus 10 years"
Very easy to implement, right?
This method allows you to add expires headers based on file type, too. You may want to do like this:
  1. ExpiresActive On
  2. ExpiresByType text/html "access plus 1 day"
  3. ExpiresByType image/gif "access plus 10 years"
  4. ExpiresByType image/jpeg "access plus 10 years"
  5. ExpiresByType image/png "access plus 10 years"
  6. ExpiresByType text/css "access plus 10 years"
  7. ExpiresByType text/javascript "access plus 10 years"
  8. ExpiresByType application/x-javascript "access plus 10 years"
As you see, adding expires headers for each file types is a bit complicated. You have to remember the mime-types of these types. That’s not always convenient. A better solution is: set the default expires headers for all file types, and modify expires headers for 1 file type only, like this:
  1. ExpiresActive On
  2. ExpiresDefault "access plus 10 years"
  3. ExpiresByType text/html "access plus 1 day"
This will add expires headers 10 years for all file types, except HTML file type (1 day). That’s good for almost blogs, because HTML file type often is the webpage itself, so you have dynamic content, make short expires headers keep your content always fresh.
Note that this rules requires mod_expires installed. This is a popular module, usually installed on almost web hosts.

Compress Text Files (CSS, Javascript)

Compress text files like CSS, Javascript or HTML files is a effective way to reduce the page’s size, and therefore, reduce page load time and bandwith. To turn on compression in .htaccess file, put the following lines into it:
  1. SetOutputFilter DEFLATE
The problem is that you don’t need to compress all files, especially image files (like jpeg, gif) because they’re alreay been compressed (using image compression algorithm). You should apply this rule for text files. So, change the code above to:
  1. <FilesMatch ".(js|css|html|htm|php|xml)$">
  2. SetOutputFilter DEFLATE
  3. </FilesMatch>
This rule requires mod_deflate installed. This module is not always installed on all hosts. I’m usingBlueHost and see it’s installed. Please check your host configuration (or ask your hosting company) to make sure it works (you can still leave them in .htaccess file, they won’t effect to other rules at all).

How to check whether or not rules were activated?

To check whether or not rules were activated on your host, you can use Firebug addon for Firefox. Make sure to activate the Net tab (the last tab) of Firebug.
After that, make some sample requests to your files and check the response headers. Here’s an example request that I made, you can see the expired date and compression method.
headers 3 Simple .htaccess Rules For Better Website Performace



Cache Control with htaccess EXPIRES BY TYPE


 / Published in: Apache
  

  1. # Cache Control with .htaccess EXPIRES BY TYPE
  2. # from http://forum.powweb.com/showthread.php?t=62786
  3.  
  4. ### turn on the Expires engine
  5. ExpiresActive On
  6.  
  7. ### expires after a month in the client's cache
  8. ExpiresByType image/gif A2592000
  9. ExpiresByType image/png A2592000
  10. ExpiresByType image/jpg A2592000
  11. ExpiresByType image/x-icon A2592000
  12. ExpiresByType application/pdf A2592000
  13. ExpiresByType application/x-javascript A2592000
  14. ExpiresByType text/plain A2592000
  15.  
  16. ### expires after 4.8 hours
  17. ExpiresByType text/css A17200
  18.  
  19. # Please note that the "A" before the numbers above stands for Access.
  20. # This means that the stopwatch starts when a client accesses the file.
  21. # You can also use "M" for modified.
  22.  
  23. # TIME CHEAT SHEET
  24. # 300 5 MIN
  25. # 600 10 MIN
  26. # 900 15 MIN
  27. # 1800 30 MIN
  28. # 2700 45 MIN
  29. #
  30. # 3600 1 HR
  31. # 7200 2 HR
  32. # 10800 3 HR
  33. # 14400 4 HR
  34. # 18000 5 HR
  35. # 36000 10 HR
  36. # 39600 11 HR
  37. # 43200 12 HR
  38. # 46800 13 HR
  39. # 50400 14 HR
  40. # 54000 15 HR
  41. # 86400 24 HR
  42. #
  43. # 86400 1 DAY
  44. # 172800 2 DAY
  45. # 259200 3 DAY
  46. # 345600 4 DAY
  47. # 432000 5 DAY
  48. # 518400 6 DAY
  49. # 604800 7 DAY
  50. #
  51. # 604800 1 WEEK
  52. # 1209600 2 WEEK
  53. # 1814400 3 WEEK
  54. # 2419200 4 WEEK
  55. #
  56. # 2419200 1 MONTH (FEBRUARY)
  57. # 2505600 1 MONTH (FEBRUARY LEAP YEAR)
  58. # 2592000 1 MONTH (APRIL, JUNE, SEPTEMBER, NOVEMBER)
  59. # 2678400 1 MONTH (JANUARY, MARCH, MAY, JULY, AUGUST, OCTOBER, DECEMBER)
  60. # 31536000 12 MONTH

Related Posts

Tips 7441110742643829519
Comments
0 Comments
Facebook Comments by Media Blogger

Post a Comment

Search

Ikuti Channel Youtube Aku Yaa.. Jangan Lupa di subscribe. Terima kasih.

Popular Posts

Translate