enable caching of images only in specific folder - .htaccess

I have the below htacess code and am wondering how i can specify it to only apply to a specific folder. in this case, only all image files in: /public_html/img/icons/
<filesMatch ".(png)$">
Header set Cache-Control "max-age=31536000, public"
</filesMatch>

Set an environment variable (using SetEnvIf) when the URL requested matches the specific folder and image type(s). Set the Header conditionally based on this env var (using the env= argument of the Header directive).
For example:
# Cache images in the "/img/icons" subdirectory
SetEnvIf Request_URI "^/img/icons/.+\.(png|jpg|gif)$" ENABLE_CACHE
Header set Cache-Control "max-age=31536000, public" env=ENABLE_CACHE
This method allows you to keep all the directives in the single .htaccess file in the document root. It also works on all versions of Apache (unlike <If> expressions that require Apache 2.4).
However, this may also be dependent on other directives you have in the .htaccess file. If, for instance, you are on Apache (as opposed to LiteSpeed) and rewriting the URL with mod_rewrite (a front-controller pattern perhaps) then you may need to test for REDIRECT_ENABLE_CACHE in the Header directive instead. (Or change your existing directives to prevent a loop of the rewrite engine, that results in the env var being prefixed with REDIRECT_.)

Related

Make 1 exception on X-Frame-Options SAMEORIGIN

I use
Header set X-Frame-Options SAMEORIGIN
in .htaccess
But i would like to have 1 html page that isn't blocked when shown in iframe on other websites.....
How can i make 1 exception?
The Header directive provides an additional argument that allows you to set the header conditionally based on whether an environment variable is set or not.
You could then set an env var when this one URL is requested. And only allow the header to be set when the env var is not set.
For example:
SetEnvIf Request_URI "^/one-page-not-blocked\.html$" DO_NOT_BLOCK
Header set X-Frame-Options SAMEORIGIN env=!DO_NOT_BLOCK
The above SetEnvIf directive sets the env var DO_NOT_BLOCK to the value 1 when the regex matches the requested URL.
The env=!DO_NOT_BLOCK argument is successful when the env var is not set (denoted by the ! prefix).
This method allows you to add additional URLs to not block by simply adding more SetEnvIf directives.

Access files outside of webroot through alias and .htaccess rule

I need to make PDF files that are stored in a folder (with subfolders) outside of the web root publically accessible by a plain URL. An alias has been created in Apache that leads this folder so what I need now is a redirect rule in .htaccess to make this work.
I have this alias: https://www.examplesite.com/certificate
The URLs that will be used to access these PDFs are for example: https://www.examplesite.com/certificate/2018/LGOIGD9E9345034GJERGJER.PDF
https://www.examplesite.com/certificate/2017/GSDFJGLKJNL345L34LSNFLSD.PDF
How should I format my redirect rule in .htaccess to decide if the file is to be downloaded or viewed in the browser?
Sorry about the noise, I found the answer by myself:
<FilesMatch "\.pdf$">
ForceType applicaton/octet-stream
Header set Content-Disposition attachment
</FilesMatch>

how to delete header cache in htaccess?

In my htaccess file there is this :
<FilesMatch "\.(js|css|pdf|txt)$">
Header set Cache-Control "max-age=7257608"
</FilesMatch>
Now, if I wanted to alter a css type file. the css will change if I refreshed the page. Other users still get the old css file because of the cache. what can I do on my side to let the users browsers recognize there is a change in the css file?
Generally rather than setting a cache age in the .htaccess, make sure you're configured to use if-modified-since which is documented in the Apache Caching Guide, using the mod_cache extension:
Generally, it's as simple as this, with exceptions written for secured resources:
LoadModule mem_cache_module modules/mod_mem_cache.so
<IfModule mod_mem_cache.c>
CacheEnable mem /
MCacheSize 4096
MCacheMaxObjectCount 100
MCacheMinObjectSize 1
MCacheMaxObjectSize 2048
</IfModule>

Forcing a download using <filesMatch> in htaccess

I'm trying to force the download of all files of one folder.
The link on the page looks like this
Click to download
And I have this snippet in my .htaccess
<filesMatch ".*uploads/documents.*">
ForceType application/octet-stream
Header set Content-Disposition attachment
</filesMatch>
I already know that the 2 lines inside the tag works, because it works when I put a .htaccess directly inside the folder where I want to force the download with the following code:
<Files *.*>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
There seems to be something which I don't understand about the filesMatch tag.
Searching more info found this code:
<FilesMatch "\.(mov|mp3|jpg|pdf|mp4|avi|wmv)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Worked for me.
Please look at the documentation for FilesMatch and Files, respectively. It clearly states
The directives given within this section will be applied to any object
with a basename (last component of filename) matching the specified
filename.
That means that in your example it matches against file.pdf. Your second example *.* matches file.pdf, however your first example .*uploads/documents.* does not. It actually can never match, since it contains a slash, which is used as a directory separator.
If you can edit the apache config
You should enclose either <Files *.*> or <Files *.pdf> (depending on what you want to enforce downloading) in a Location directive:
<Location "/uploads/documents/">
<Files *.*>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
</Location>
If you cannot edit the apache config
Unfortunately, the Location directive is not allowed inside .htaccess files. Just create a .htaccess inside your /uploads/documents/ directory.
This code is perfect if you don't use - in the file name!
For example, for name-1.mp3, change to name1.mp3
<FilesMatch "\.(mp3|avi)$" >
ForceType application/octet-stream
Header add Content-Disposition "attachment"
</FilesMatch>
Clear your browser and check it.

X-Robots noindex specific page in .htaccess

Can I 'noindex, follow' a specific page using x robots in .htaccess?
I've found some instructions for noindexing types of files, but I can't find instruction to noindex a single page, and what I have tried so far hasn't worked.
This is the page I'm looking to noindex:
http://www.examplesite.com.au/index.php?route=news/headlines
This is what I have tried so far:
<FilesMatch "/index.php?route=news/headlines$">
Header set X-Robots-Tag "noindex, follow"
</FilesMatch>
Thanks for your time.
It seems to be impossible to match the request parameters from within a .htaccess file. Here is a list of what you can match against: http://httpd.apache.org/docs/2.2/sections.html
It will be much easier to do it in your script. If you are running on PHP try:
header('X-Robots-Tag: noindex, follow');
You can easily build conditions on $_GET, REQUEST_URI and so on.
RewriteEngine on
RewriteBase /
#set env variable if url matches
RewriteCond %{QUERY_STRING} ^route=news/headlines$
RewriteRule ^index\.php$ - [env=NOINDEXFOLLOW:true]
#only sent header if env variable set
Header set X-Robots-Tag "noindex, follow" env=NOINDEXFOLLOW
FilesMatch works on (local) files, not urls. So it would try to match only the /index.php part of the url. <location> would be more appropriate, but as far as I can read from the documentation, querystrings are not allowed here. So I ended up with the above solution (I really liked this challenge). Although php would be the more obvious place to put this, but that is up to you.
The solution requires mod_rewrite, and mod_headers of course.
Note that you'll need the mod_headers module enabled to set the headers.
Though like others have said, it seems better to use the php tag. Does that not work?
According to Google the syntax would be a little different:
<Files ~ "\.pdf$">
Header set X-Robots-Tag "noindex, nofollow"
</Files>
https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag

Resources