Using htaccess or a php file to cache only static files - php

I am looking for a way to implement cache control & expire headers to my images/css/js files. I looked for tutorials & only found ways to add cache control using file extensions. The code below is the one got from http://betterexplained.com/
<FilesMatch "\.(jpg|jpeg|png|gif)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
This sets all the images of the mentioned extensions to be cached for one week. However I don't want all the images to be cached, because there are some images that are upload by the users. I only want my static images eg: logo, icons used for buttons to be cached. It would be great if someone could point out a way or a tutorial on how this could be achieved.

or again you could just move the htaccess file in the folder that will be cached and save the user image in another :D

You can change the FilesMatch filter to include a part of a path. I assume the user images are in another directory than your static images.
i.e.
<FilesMatch "^/staticdir/.+\.(jpg|jpeg|png|gif)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

You can make your regex more restricitive by including only only know images for caching:
<FilesMatch "(logo|icon|favicon|header)\.(ico|jpe?g|png|gif)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

Related

PHP Includes Stopped Working

I have a website that uses a number of simple PHP include commands to call up scripts on multiple pages. The most simple example is the shared navigation menu that can be dynamically updated as needed and called upon with the following line of code:
<?php include("nav.php"); ?>
This has worked without issue for several years. However, our webhost (Arvixe) has just moved us to a new server and suddenly the includes have stopped working altogether. First thing I did to try to resolve the issue was upload the .htaccess file from the original server, but that has had no effect. I've been working with their support team for almost a week now, but they can't figure out what's going on and I'm not sure how to better communicate what is needed, at this point.
Here's the .htaccess file content:
AddHandler server-parsed .html
AddHandler application/x-httpd-php .html
# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "max-age=0, public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "max-age=0, public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "max-age=0, private"
</filesMatch>
</ifModule>
# END Cache-Control Headers
Header set Set-Cookie "VC-NoCache=1; max-age=900; path=/"
Header set VC-NoCache "1"
Any idea what might have changed on their end that needs to be rectified? Thanks.

htaccess opening php files as downloads

i have had a search around but haven't been able to find a similar problem.
When i add the following to my htaccess file (i want to restrict upload types to my server)
ForceType application/octet-stream
Header set Content-Disposition attachment
<FilesMatch "(?i)\.(doc?x|txt|xls?x|csv|pdf|ppt?x|zip|gif|jpe?g|png)$">
ForceType none
Header unset Content-Disposition
</FilesMatch>
Header set X-Content-Type-Options nosniff
I then refresh my page it then opens the download dialog box instead of showing the php page. I though it might be the deflate mod compressing the htaccess onto 1 line but im not so sure now.
could anyone point me in the right direction?
Thanks
Thanks to the comments above i have sorted the problem. it was my mistake

How can i add cache control code to htaccess?

How can I add cache control code to .htaccess?
My site is slow and I found 35 static components without a far-future expiration date.
How can I add expiration data for that? I was informed that this could be done in the .htaccess file but I don't know how.
You can use something like this:
# Cache Files
<filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
This caches any file ending with ico|pdf|flv|jpg..... for 604800 seconds, which is one week, you can change that number to cache for however long you feel you need.

FilesMatch downloading files instead of loading

Rightio...I am about to add an image upload to my site. After reading some security blogs I have added the following to my htaccess file.
ForceType application/octet-stream
<FilesMatch "(?i)\.jpe?g$">
ForceType image/jpeg
</FilesMatch>
<FilesMatch "(?i)\.gif$">
ForceType image/gif
</FilesMatch>
<FilesMatch "(?i)\.png$">
ForceType image/png
</FilesMatch>
When I browse to a PHP page it downloads the file.php instead of showing the page in the browser.
Any ideas would be much appreciated.
I am working on MAMP locally.
Many thanks
Lewis
When I browse to a PHP page it downloads the file.php instead of showing the page in the browser
That's because you have this line:
ForceType application/octet-stream
by itself, without any condition. That means, everything should have the mime-type application/octet-stream, which browsers see and understand as binary content that should be downloaded. Not sure how this is a security improvement as it makes it so your entire site can be downloaded and viewed as code, as opposed to any dynamically generated content.
You have to put that .htaccess code in a directory that only has image files. If you don't have your images in their own directory, create a directory for them and put them there (and update any links to them).

Expires in 1 month header on all files except .php files

To speed up my website I have read about the Expires header, how to implement it in Apache, and how to only do so for certain file types. However I want to set a one month expiry on all files except .php files (webpages).
So how do I do this with .htaccess? I have a PHP script that uses filemtime to change the filename of CSS, JS files when they are edited, so that they are re-downloaded (eg if a file changes at xxxxxxx in Unix time, it changes the filename to /resource.css?recache=xxxxxxxx when it is requested). So that's taken care of. As images etc. don't change often, I want all of them to be cached for a month.
Also, what is the browser support for the Expires header?
Use the mod_expires in your Apache configuration files. For example:
# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
# Set up caching on media files for 1 month
<filesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|gif|jpg|jpeg|png|swf)$">
ExpiresDefault A2592000
Header append Cache-Control "public"
</filesMatch>
You can read more here

Categories