bradfrankshaw Posted July 8, 2011 Share Posted July 8, 2011 Hi all - our site is being hosted on a server where I have no access to the .htaccess file and using YSlow I've seen I need to set expires for my css, images, flash etc. I've coded a header expires file as the first thing on my page (before any output) header("Cache-Control: must-revalidate"); $offset = 60 * 60 * 24 * 730; $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT"; header($ExpStr); This seems to set an expire on the php page itself, but how do I code into the page to set expires on the css, images, swfs etc? I tried <?php // -----------CSS------------------------------------------------------ /* ob_start ("ob_gzhandler"); header ("content-type: text/css; charset: UTF-8"); header ("cache-control: must-revalidate"); $offset = 60 * 60 * 24 * 730; $expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT"; header ($expire); ?> <?php // ----------PHP------------------------------------------------------ if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); header ("content-type: text/html; charset: UTF-8"); header ("cache-control: must-revalidate"); echo $offset = 60 * 60 * 24 * 365; $expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT"; header ($expire); ?> <?php // -----------HTML------------------------------------------------------ ob_start ("ob_gzhandler"); header ("content-type: text/HTML; charset: UTF-8"); header ("cache-control: must-revalidate"); $offset = 60 * 60 * 24 * 365; $expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT"; header ($expire); ?> <?php // -----------GIF------------------------------------------------------ ob_start ("ob_gzhandler"); header ("content-type: image/gif; charset: UTF-8"); header ("cache-control: must-revalidate"); $offset = 60 * 60 * 24 * 730; $expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT"; header ($expire); ?> ...etc just to see what would happen. Apparently this causes the page to freak because there are multiple file type headers and the server doesn't know what the actual file type is of the page as it has so many and just displays plain text. So, is it possible to put multiple expires straight into the code without resorting to using the .htaccess file? cheers! frank Quote Link to comment https://forums.phpfreaks.com/topic/241423-how-to-write-header-expires-for-images-css-swf-etc-without-htaccess/ Share on other sites More sharing options...
requinix Posted July 8, 2011 Share Posted July 8, 2011 You'd have to run the files through a PHP script, but you'd need mod_rewrite and a .htaccess for that (unless you wanted to change the links to all be like /cache.php?/images/foo.gif). No .htaccess at all? Why not? And Apache isn't sending caching headers? What headers is it sending? Quote Link to comment https://forums.phpfreaks.com/topic/241423-how-to-write-header-expires-for-images-css-swf-etc-without-htaccess/#findComment-1240240 Share on other sites More sharing options...
bradfrankshaw Posted July 11, 2011 Author Share Posted July 11, 2011 Hi there - thanks for your reply - we're on FastHosts and looking up .htaccess on their support pages produces this: The .htaccess file The .htaccess file allows customised configuration settings to be applied to a specific directory (and any subdirectories thereof) on a Linux webserver. Note: The .htaccess file described in this article relates to Linux servers only, and should not be confused with the file of the same name created by Frontpage Extensions on a Windows server. The .htaccess file will allow changes to the folllowing configurations: Mod_rewrite This allows you to manipulate URL's to make them more user and search engine friendly. More information regarding the use of this feature can be found in our article: Mod_rewrite on Linux web servers. Password Protection It is not possible to add password protection directly by creating a .htaccess file, however this feature can be created through your Fasthosts control panel. Custom error pages It is not possible to add custom error pages by creating a .htaccess file, however this feature can be created through your Fasthosts control panel. Frontpage Extensions If you are using Frontpage extensions to publish your website a .htaccess file will be created. This will be managed and maintained by Frontpage. All other features of the .htaccess file have been disabled on our shared hosting servers. What code can I use to find out what headers are being sent? Sorry, my php is a bit rusty cheers frank Quote Link to comment https://forums.phpfreaks.com/topic/241423-how-to-write-header-expires-for-images-css-swf-etc-without-htaccess/#findComment-1241213 Share on other sites More sharing options...
requinix Posted July 12, 2011 Share Posted July 12, 2011 Uh, if you're using a Linux server then you can have a .htaccess. With mod_rewrite. So... are you saying you're using Windows? Quote Link to comment https://forums.phpfreaks.com/topic/241423-how-to-write-header-expires-for-images-css-swf-etc-without-htaccess/#findComment-1241919 Share on other sites More sharing options...
bradfrankshaw Posted July 13, 2011 Author Share Posted July 13, 2011 Fasthosts server box is Linux yes, but according to their support page quoted, they limit as to what can be done on the .htaccess file "All other features of the .htaccess file have been disabled on our shared hosting servers". So the original question was: if this is so and I can't put header stuff on the .htaccess file, is there a way of creating a php file that lets the server know what to do about setting expires for images, swf files etc? cheers frank Quote Link to comment https://forums.phpfreaks.com/topic/241423-how-to-write-header-expires-for-images-css-swf-etc-without-htaccess/#findComment-1242124 Share on other sites More sharing options...
requinix Posted July 13, 2011 Share Posted July 13, 2011 Yeah... It's what I said: you create a PHP script and siphon whatever files you choose through it, which sends the content after some appropriate caching headers. Then you put some URL rewriting rules in the .htaccess for it to take effect behind the scenes. Quote Link to comment https://forums.phpfreaks.com/topic/241423-how-to-write-header-expires-for-images-css-swf-etc-without-htaccess/#findComment-1242125 Share on other sites More sharing options...
bradfrankshaw Posted July 13, 2011 Author Share Posted July 13, 2011 Ok thanks - assuming my php is very rusty/I didn't achieve this level of coding when I was doing PHP/I'm a dumbass, would it be possible to lay this out as code/an example so that I can see how it all works and anyone reading this thread in the future will find it useful when they're search for this subject/topic on google - I did that and found more questions than answers! cheers in advance if you can do this frank Quote Link to comment https://forums.phpfreaks.com/topic/241423-how-to-write-header-expires-for-images-css-swf-etc-without-htaccess/#findComment-1242131 Share on other sites More sharing options...
requinix Posted July 13, 2011 Share Posted July 13, 2011 If there's a need for some code then I'll either write something or point you in the direction to go. What did YSlow tell you about caching? Do you know what headers are being sent with those files? Quote Link to comment https://forums.phpfreaks.com/topic/241423-how-to-write-header-expires-for-images-css-swf-etc-without-htaccess/#findComment-1242344 Share on other sites More sharing options...
bradfrankshaw Posted July 14, 2011 Author Share Posted July 14, 2011 Hi there - yes any code or a point in the right direction would be very helpful thanks. I learn from example rather than bulk text and then using and reusing that example in different forms to gain understanding. Y Slow gave our site a grading of B - with F for "Add Expires headers". 13 static components without a far-future expiration date, css, js, gif, pngs, ico and swfs are listed. Although two of these are out of my hands, ga.js (google) and get_flash_player.gif (adobe). Does that help? cheers frank Quote Link to comment https://forums.phpfreaks.com/topic/241423-how-to-write-header-expires-for-images-css-swf-etc-without-htaccess/#findComment-1242627 Share on other sites More sharing options...
requinix Posted July 14, 2011 Share Posted July 14, 2011 It would help to know the specific headers (I don't know much about YSlow), or the website so I can check myself. Generally Apache sends some caching headers so you don't need to do anything else. Otherwise you can try a FileETag INode MTime Size in the .htaccess; FileETag is part of the core so it could be that they allow it without specifically mentioning so. Otherwise, to do it yourself, caching can be very complicated. In effect, your .htaccess has RewriteCond %{REQUEST_FILENAME} -f RewriteRule \.(list|of|file|extensions)$ cache.php?%{REQUEST_FILENAME} [L] and cache.php would have $file = $_SERVER["QUERY_STRING"]; validate $file send the file MIME type generate and send the last modified date for the file generate and send the correct etag for the file // etag if there's a $_SERVER["HTTP_IF_NONE_MATCH"] and it matches the etag { send a 304 Not Modified stop } // last modified if there's a $_SERVER["HTTP_IF_MODIFIED_SINCE"] and it comes on/after the date { send a 304 Not Modified stop } send the file size send the file content There's actually quite a bit more that should happen to follow the HTTP standard, like checking the request method and sending 412 Precondition Failed responses. That's why it's best to try to get Apache to do the caching for you. Quote Link to comment https://forums.phpfreaks.com/topic/241423-how-to-write-header-expires-for-images-css-swf-etc-without-htaccess/#findComment-1242808 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.