ExpiresDefault on dynamic url? - apache

I have different types of dynamic urls like
/dynamic/image/day/blah/blah/blah.jpg
/dynamic/image/week/blah/blah/blah.png
/dynamic/image/month/blah/blah/blah.gif
this urls are not actual filesystem paths but served by a php script ..
IN httpd.conf :
whey I try with the following configuration :
ExpiresActive On
ExpiredDefault A0
<LocationMatch */image/day/*>
ExpiresDefault A86400
</LocationMatch>
<LocationMatch */image/week/*>
ExpiresDefault A604800
</LocationMatch>
It applies last one 'A604800'(after week) expiry header to both type of files .. means even for /day/ type of urls the last ExpiresDefault is overriding to all the above ExpiresDefault .
I have also tried putting FilesMatch inside LocationMatch like this :
<LocationMatch */image/week/*>
<FilesMatch "\.(jpg|jpeg|png)">
ExpiresDefault A604800
</FilesMatch>
</LocationMatch>
But now not a single directing inside FilesMatch is executing meanse Expires header is not set ..
I have also tried using Header set ,, but looks like it doesn't follow Locationmatch condition but just last Header Set whould apply to all ..
Those are dynamic url , not actual directories , so I can't use different htaccess for different day,week etc ..
Any Solution ..?

Related

Setting cache control headers for Apache proxy on Elastic Beanstalk

I have an elastic beanstalk server running an Apache proxy on Amazon Linux 2. I want to set the cache-control header on my index.html file to public, max-age=0.
In order to update my Apache config I understand I can add a config file to .platform/httpd/conf.d. In my first attempt I created this file:
<FilesMatch "index\.html">
Header set Cache-Control "public, max-age=0"
</FilesMatch>
This did not work. Looking around I think this is because the directives I have used are intended for Apache's htaccess file or within a <VirtualHost> section of the conf file.
Any ideas how I can get this working? Note I found this answer had some useful information.
EDIT: I also tried this in a conf file (it didn't set any cache-control headers).
<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresDefault "access plus 2 days"
ExpiresByType text/html "access plus 600 seconds"
</IfModule>
EDIT: I also tried switching to nginx. I added a file .platform/nginx/conf.d/cache.conf, with the following contents
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 2d;
add_header Cache-Control "public, no-transform";
}
After deployment and a server restart, no cache-control headers were set on the files specified.

Apache mod_headers cannot unset header on a path

I am trying to set a header using mod_headers in Apache in all cases EXCEPT a certain path. I've tried each of the three variations below to do so, but none of them seem to work properly to exclude the path. In ALL cases I get the header for all requests, including those that match the example path, e.g.: http://example.com/charts/24_hour_commodity/450/300
<VirtualHost *:8200>
...
SetEnvIfNoCase Request_URI "^/charts/.*" frameallow
Header set X-Frame-Options SAMEORIGIN env=!frameallow
...
</VirtualHost>
Or:
<VirtualHost *:8200>
...
Header always set X-Frame-Options SAMEORIGIN
<LocationMatch "^/charts">
Header always unset X-Frame-Options
</LocationMatch>
...
</VirtualHost>
Or
<VirtualHost *:8200>
...
Header always set X-Frame-Options SAMEORIGIN
<Directory "/full/path/to/charts">
Header always unset X-Frame-Options
</Directory>
...
</VirtualHost>
#tried both with and without the 'always' in all configs
Can anyone help me figure out why the header is set in the first example or not unset in the following two? Any one working solution would be enough...
UPDATE:
After reading about order of processing on the Apache site, I tried using conditional blocks instead. Neither of those work either:
<If "%{REQUEST_URI} =~ m#^/charts#">
Header unset X-Frame-Options
</If>
Or
SetEnvIfNoCase Request_URI "^/charts" frameallow
<If "reqenv('frameallow') == 1">
Header unset X-Frame-Options
</If>
So, still broken. Must be something about the Header statements not firing after a certain point in processing. Or the ones int he conditional somehow firing before the main one and being overridden. Cannot find a way to debug it down to the root cause though.
Responses header with expression
Header always set Access-Control-Allow-Origin * "expr=%{REQUEST_URI} =~ m#^/specialPath$#"
this may add header wen the expr = true
http://httpd.apache.org/docs/current/mod/mod_headers.html
at the bottom of the section Header Directive

Header Expires not working

In httpd.conf the last lines are:
ExpiresActive On
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresDefault "access plus 3 days"
</FilesMatch>
When I run YSlow it says all my CSS files (and others) do not have expiration set. The CSS files are in subdirectories but I believe "\." should match all files regardless of subdirectory.
Apache is set up for virtual hosts, but the above directives are outside of vhosts section at the very bottom of httpd.conf.
There is a directive for LoadModule expires_module modules/mod_expires.so in the .conf file.
The site is on AWS running AWS Linux and Apache2. I restarted apache before checking if it worked.
In answer to your question, this works for me in my httpd.conf:
LoadModule expires_module /usr/lib/apache2/modules/mod_expires.so
ExpiresActive On
<filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresDefault "access plus 3 days"
</filesMatch>
Here are a few things to try:
1) Make sure mod_expires.so is where you think it is. You can see what modules are being included by using this in your terminal: apache2 -M.
2) Make sure your directives apply to the files you're interested in (i.e. not getting overwritten by a .htaccess file somewhere farther down the line)
3) Make sure the LoadModule directive is in your httpd.conf file
4) In your FilesMatch regex, you should escape the dot with a backslash otherwise you're matching any character. Not a big issue, but you'd end up applying the rule to non-static pages like "site.com/politico" (note the ico ending) for example.

Apache caching with .htaccess problem (incorrect Date header?)

I am using .htaccess to cache the js / css / etc files on the website. Everything works fine, but for some reason my pages get cached, too. I looked into the headers, and it works like this:
Browser requests a page http://poko.lt/up
Server responds with 301 to http://poko.lt/up/ (with Date and Last Modified headers equal to current time)
Browser requests http://poko.lt/up/
Server responds with the page, and with Date and Last Modified headers equal to my last forced refresh (like, yesterday), but Cache-control header is fine (max-age=0)
I get the old version of the page :/
My .htaccess looks like this:
# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
# 1 YEAR
<FilesMatch "\.(jpg|jpeg|png|gif|svg|eot|ttc|ttf|otf)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
Header unset Last-Modified
Header unset ETag
FileETag None
</FilesMatch>
# 2 WEEKS
<FilesMatch "\.(css|js|swf)$">
ExpiresDefault A1209600
Header append Cache-Control "proxy-revalidate"
Header unset Last-Modified
Header unset ETag
FileETag None
</FilesMatch>
# NO CACHE
<FilesMatch "\/$">
ExpiresDefault A0
Header append Cache-Control "proxy-revalidate"
Header unset Last-Modified
Header unset ETag
FileETag None
</FilesMatch>
And, one can test it with the website http://poko.lt (the three red icons in the top middle of the page are the ones causing the problems). I am using FF4, and checking headers with Firebug.

Make all files in a folder (and subfolders) expire in a year using .htaccess

I want to set expiration headers for all the files in a "media" folder. A file looks like this: media/css/db38a57a8fa9de240b3ed5eb8be83691. I want it to be served with headers specifying it is to expire in a year or more.
How do I do that with .htaccess?
First , is mod_expire enabled in apache ? you can setup expiration like this if it's enabled
<ifmodule mod_expires.c>
ExpiresDefault "modification plus 1 year"
</ifmodule>
Files will expire 1 year after they have been last modified , if you whant it based on access you can set it like so :
<ifmodule mod_expires.c>
ExpiresDefault "access plus 1 year"
</ifmodule>
Have a look here.