I'm using apacher web server to stre the static content (like images, css etc.) with my website. Below is my apache configurations set as below :
<IfModule mod_expires.c>
ExpiresActive on
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf|html)$">
ExpiresActive On
ExpiresDefault "access plus 30 minutes"
</FilesMatch>
</IfModule>
<IfModule mod_headers.c>
<FilesMatch ".+\.(ico|jpg|jpeg|png|gif|js|css|swf|html|woff)$">
Header set X-XSS-Protection "1; mode=block"
Header set X-Content-Type-Options nosniff
</FilesMatch>
</IfModule>
Please note that the same apache configuration is working in test region, but not working in prod region for same jpeg images.
The cache works well for woff,css, js,png files, but doesnt work for my few jpeg images which are loaded from specific folder for one of the website screen. Any idea why?
Thanks in advance.
In our application we have both gif and jpg images for the same set of images. I've both gif and jpg images in my local server. Hence I opened all the below gif images in MSPaint and stored it as .jpg.
Next_disabled.jpg
Next.jpg
Prev_disabled.jpg
Prev.jpg
Then I've deployed these images to apache where static content is present, followed by apache restart. Then the issue was resolved and the images are showing properly.
Related
The error that I get is from PHP because APACHE is tryng to pass the .zip file through the PHP interpreter.
But this post is not regarding any PHP script, or setting up headers from PHP to make the file download. It is about preventing APACHE from passing the file through PHP and generate a download instead.
I have found a few answers that involve adding File Exceptions on the .htaccess but still without success and I get the follwing Parse error message:
Parse error: in /home/user/public_html/downloads/files/1/test-file-132110.zip on line 431
These are the .htaccess instructions that I found that work:
<Location "/downloads/files/">
<Files *.*>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
</Location>
The previous example is not so great because I want to chose specific extensions, lile doc, zip, etc. So I tried these instead:
<Location "/downloads/files/">
<FilesMatch ".(?i:doc|docx|xls|xlsx|ppt|pptx|zip|rar)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
</Location>
or this, without specifying a location:
<FilesMatch "\.(doc|docx|xls|xlsx|ppt|pptx|zip|rar)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
I want browsers to cache html files. I set up Apache2.4 and confirm css, js, png are cached, but html files are never cached.
Chrome 90 and Edge 90
Apache 2.4
/etc/httpd/conf/httpd.conf
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 30 days"
</IfModule>
Hi try to add ExpiresByType text/html "access plus 7200 seconds". Hope it works !
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.
Do the cache control directives need to specify if a query string is present in the http request for proper matching?
Currently using query strings for static asset versioning, and disabled Etags for those - but the directives are not taking effect whatsoever. Looking at the headers, Etags are still used and there is no cache-control defined. Gmatrix and Lighthouse both state that static assets have no cache control set.
Example file requested: app.js?v=1.3.5
Here is the .htaccess file, followed by the troubleshooting steps
<IfModule mod_headers.c>
# One month for media files and icons
<FilesMatch "\.(ogg|mp3|ico|jpg|jpeg|png|svg|webp|webmanifest|xml)\?.*$">
Header set Cache-Control "max-age=2592000, public"
Header unset ETag
FileETag None
</FilesMatch>
# One week for CSS/JS files except service worker file
<FilesMatch "^(?!sw).+\.(css|json|js)\?.*$">
Header set Cache-Control "max-age=604800, public"
Header unset ETag
FileETag None
</FilesMatch>
# No cache for HTML files (checks with server for changes else serves cache)
<FilesMatch "\.(html)$">
Header set Cache-Control "no-cache, must-revalidate"
</FilesMatch>
<FilesMatch "^(sw\.js)$">
Header set Cache-Control "no-cache, no-store, must-revalidate"
</FilesMatch>
</IfModule>
Troubleshooting steps
Directives don't seem to affect html pages either so assuming this may be a config or syntax issue.
Check syntax with validator: https://www.lyxx.com/freestuff/002.html
Ensure AllowOverride is set to All in /etc/apache2/sites-available/mysite.conf
Try setting AllowOverride to All in /etc/apache2/apache2.conf for /var/www/
Try adding the directives directly in /etc/apache2/sites-available/mysite.conf
Restart apache after changes: service apache2 restart
The mod_headers module was not activated!
Use apachectl -M to check for active modules (Debian/Ubuntu).
If headers module not listed:
Use a2enmod headers to activate the headers module.
Restart apache2 with service apache2 restart to implement changes.
At this point was able to test whether the query string needs to be added in the FilesMatch expression.
Turns out the query string needs to be omitted from the expression.
Use: \.(js)$
Don't use: \.(js)\?.*$
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.