Is it possible to deactivate caching Rules in htacces by IP matching? - apache

Is it possible to deactivate caching Rules like mod_expires.c, mod_headers.c in htaccess by IP matching like < FilesMatch > or < IfModule >?
I did not find any possible solution so far. Any hints?

If you want to deactivate caching you can use the following in your .htaccess file:
ExpiresActive On
ExpiresDefault A1
Header append Cache-Control must-revalidate
Just make sure you place it at the top of your .htaccess file and clear your cache upon entering it. From that point forward it will also ask the cache to re-validate.

Related

How to enable Expect-Ct on apache using .htaccess

i want to enable Expect-Ct on my website.
From searching around i creaded code on my own from searches.
<IfModule mod_headers.c>
Expect-CT: max-age=86400, enforce, report-uri="https://foo.example/report"
</IfModule>
I want to ask if this is right or not and what is report uri ? it can be any random link or something else.
for example my website is testwebsite.com then report uri should be testwebsite.com/report ? and how can i check reports ?
This is a good question; while the general syntax form is explained in the link provided in comments, it doesn't explain how to correctly apply this header in the .htaccess or httpd.conf Apache files.
Through trial and error I found this works:
<IfModule mod_headers.c>
Header set Expect-CT enforce,max-age=2592000,report-uri="https://foo.example/report"
</IfModule>
Note that there should not be any white space in the "data" part.
Also note that the output detected by such things as redbot.org does not show exactly the same thing.

Moving from development to production and FA icons are now squares

Development
Production
I've read a ton of other posts, and 99% of them boil down to path issues. But if you look at my site you can see that it is properly loading the icon file:
Request URL:http://www.blackrhinounion.com/wp-content/themes/Centum/font/fontawesome-webfont.woff2?v=4.3.0
Request Method:GET
Status Code:304 Not Modified
So I'm thinking something else is the culprit here?
(Edit) Attempted in .htaccess:
# Allow access from all domains for webfonts.
# Alternatively you could only whitelist your
# subdomains like "subdomain.example.com".
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
And it still didn't fix it.
Figured it out.
WP_SITEURL had a www in it, while the WP_HOME didn't. That caused the Cross-Origin issue, removing the www fixed everything.

Control Cache Expiration For Custom File On Server

We have files that we serve to a Native Windows OS applications from our server. The files can change every minute so we need to ensure the user is downloading the latest file.
We've found that users on Portable WiFi's tend to get served an older file. So we are changing our servers .htaccess file expirations for certain files.
We serve a custom file type (.ebc) and the files contents are sent over HTTP as plain text. In this case should we use ExpiresByType text/ebc "access 1 minute"?
Will changing .htaccess cache control affect Portable Wifi caching or will this only affect browsers?
Should mod_expires / mode_headers occur code occur before redirects and rewrites? I've discovered before that you should perform certain .htaccess code operations before others (such as place redirects at the top of the file).
Heres my code:
RedirectMatch (?i)^/wp-content/uploads/2014/10/a.exe http://www.website.com/wp-content/uploads/2014/10/b.exe
## EXPIRES CACHING Should we place this before mode_rewrite or after? ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/ebc "access 1 minute"
</IfModule>
## EXPIRES CACHING ##
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
There are 3 questions here, so I'll attempt to answer them.
We serve a custom file type (.ebc) and the files contents are sent over HTTP as plain text. In this case should we use ExpiresByType text/ebc access 1 minute
That should be fine, as long as you have the text/ebc mime-type properly set on your server.
Will changing .htaccess cache control affect Portable Wifi caching or will this only affect browsers
I don't really know what "Portable Wifi caching" is. These headers are targeted at browsers only. If a custom application is downloading these files, it could be implementing its own caching and so these headers might get ignored.
Should mod_expires / mod_headers code occur before redirects and rewrites?
I'd put it before the redirects but only from a logical point of view. These are not like RewriteRules and think they get evaluated separately.
Additionally, I'll add that caching is difficult and once a file has left your server it can be hard to force an update. Different browsers behave different ways and I've come across configurations that work one place and not another.
I would additionally consider two other approaches to what you're attempting.
Firstly, don't cache your files at all:
<FilesMatch "\.ebc$">
Header set Cache-Control no-cache
Header set pragma no-cache
</FilesMatch>
Secondly think about implementing a cache-busting mechanism. If the file is linked from somewhere, try and make sure that link is changed (normally a querystring with a timestamp suffices) each time the file changes. You obviously then need to make sure whatever contains the link also isn't being cached.
An easier solution I used in the past was adding a parameter to the downloadable files.
For example, if the file you're serving is
http://www.domain.tld/file.pdf
then you can create the following link:
http://www.domain.tld/file.pdf?d486dFyg
The question mark and whatever comes after it (random) will be ignored but it will guarantee that the user will always download the latest version, as the URL will be different (because of the random always being different of course).
The downloaded file on the user's computer will just be file.pdf so absolutely no downside.
EDIT: I noticed some reference to WordPress in your question, which is PHP, so you can use the rand() function to append the random part: http://php.net/manual/en/function.rand.php

Proper use of Header set Connection Keep-Alive (Apache)

<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
In order to make files reloading bit faster, a server needs the Keep Alive enabled in Apache. Some questions though:
Q1) does it matter where (top or bottom of htacces) these three lines go?
Q2) if I omit the ifModule above and below the Header set Connection keep-alive then it seems still to work so is it necessary / what does the ifModule do?
Q3) on the bottom of my htacces there is already a section with ifModule mod_headers.c and all sorts of FilesMatch in there (static caching of files per filetype like css js html etc. can the Header set Connection keep-alive go in there?
Q1) It only matters if there are other lines in the file that may override it. In general, whatever comes last wins.
Q2) ifModule is a basic conditional that allows you to specify configuration if the indicated module is loaded or not
Q3) Yes, it can go in there. A conditional is a conditional, so unless you have a rather complex module-dependent configuration, you will only really need one ifModule block for any given module in a particular file

Set Content-Disposition header to attachment only on files in a certain directory?

I've got this this rule in my htaccess file to force linked files to download rather than open in the browser:
<FilesMatch "\.(gif|jpe?g|png)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Is there a way to alter the RegExp so it only applies to files in a certain directory?
Thanks
Like #gumbo said, put the .htaccess file in the highest level folder you want to affect. and those settings will trickle down to sub folders. You may also want to make sure the headers module is enabled before using this in your htaccess file. The following line will generate an error if the headers module is not enabled:
Header set Content-Disposition attachment
here's an example that forces download of mp3 files only if the headers module is enabled:
<IfModule mod_headers.c>
<FilesMatch "\.(mp3|MP3)$">
ForceType audio/mpeg
Header set Content-Disposition "attachment"
Allow from all
</FilesMatch>
</IfModule>
Note: it does not enable the module, it just ignores anything inside the IfModule tags if the module is not enabled.
To enable apache modules you'll either need to edit your httpd.conf file or in wamp server you can click the wamp tray icon and select "Apache -> Apache Modules -> headers_module" or make sure it is checked.
You will probably need to put the directives in the .htaccess file in the particular directory.
Put it in a <Location> directive, and/or modify the regex to exclude slashes or as appropriate.