| | |||||
| ||||||||
| CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| NamePros Member Join Date: Mar 2006 Location: (US) Missouri
Posts: 70
![]() | Make browsers cache your static files with PHP Hey, I had an issue where I needed to make browsers cache my static content (images, css, javascript, etc.) to reduce the server load. However due to the incompetent host I was unable to use Apache's EXPIRES_MODULE, so I came up with this solution: ????: NamePros.com http://www.namepros.com/code/679687-make-browsers-cache-your-static-files.html Step 1: Create the following directory structure: (static/images, static/css, static/javascript) that is publicly accessible; i.e. http://www.example.com/static/images/xample.jpg Step 2: Add the following to your .htaccess: Code: RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(images|css|javascript)/(.*)$ static/static.php?requested_file=$1/$2 [L] PHP Code: * note this was just a quick and dirty script that has not been optimized in any way, it was tested with the latest version of Firefox and IE. If you see any issues let me know
Last edited by Hobnob; 09-29-2010 at 05:11 PM.
Reason: lol, forgot step 4
|
| | |
| | #2 (permalink) |
| If only you knew... Join Date: Oct 2005 Location: Inside your head...
Posts: 990
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Thanks for sharing Nice code
__________________ --- The greatest truths ever told, and the greatest lies ever told, all consist of exactly the same three words: "I LOVE YOU" --- The best say little, only say what is important.....then they shut up and sit down. |
| | |
| | #4 (permalink) |
| Account Suspended Join Date: Dec 2008 Location: Boston, Ma
Posts: 650
![]() ![]() ![]() ![]() ![]() ![]() | ...or just use: Code: <filesMatch "\.(ico|gif|jpg|jpeg|png|flv|avi|mov|mp4|mpeg|mpg|mp3|pdf|doc|css|js|html|bmp|js|css)$"> Header set Cache-Control "max-age=86400" </filesMatch> Your code would waste too much loading time. Good thinking, but needs work. |
| | |
| | THREAD STARTER #5 (permalink) |
| NamePros Member Join Date: Mar 2006 Location: (US) Missouri
Posts: 70
![]() | Some people don't have the ability to use the Header set (like the customer I wrote that for). As far as the loading time, it's nothing significant and it's less of a load than not caching... But I'm glad you posted that, it's probably easier for most people. |
| | |
| | #6 (permalink) |
| Account Suspended Join Date: Dec 2008 Location: Boston, Ma
Posts: 650
![]() ![]() ![]() ![]() ![]() ![]() | Actually, it works perfect with Chrome, IE7, IE8 and FF3 out of the box and this spead up the loading of Glamourislife.com from 3.6 seconds to 0.13 secnds on a 20 Mb/s connection. For sites using worpress or anything else witha ton of included files, this is vital. ????: NamePros.com http://www.namepros.com/showthread.php?t=679687 Code: <FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|avi|mov|mp4|mpeg|mpg|mp3|pdf|bmp|js|css|flv|swf|doc)$"> Header set Cache-Control "max-age=7200" </FilesMatch> <FilesMatch "\.(xml|txt)$"> Header set Cache-Control "max-age=172800, public, must-revalidate" </FilesMatch> <FilesMatch "\.(html|htm)$"> Header set Cache-Control "max-age=7200, must-revalidate" </FilesMatch> |
| | |