First the good news: This is me asking, so you know the identical question has not been asked 84 times in the past week.
Then the bad news: This is me asking, so I am not likely to step in with an answer, saving everyone else the trouble.
And the further bad news: This is a "why" question, not a "how-to" question.
Background: In an
earlier thread [webmasterworld.com] I was asking about using FilesMatch envelopes to contain all RewriteRules pertaining to image files. I ended up doing this, and overall it seems to work.
More background: Lately I've been vexed with referer-less requests for isolated image files. Probably has to do with the New ::cough-cough:: Improved ::cough-cough:: Image Search. But unless there's a specific UA involved, I can't be sure.
I ended up making this rule:
RewriteCond %{HTTP_REFERER} ^-?$
RewriteCond %{HTTP_USER_AGENT} ! {short list of authorized search engines here}
RewriteCond %{REQUEST_METHOD} !HEAD
RewriteRule ^(ebooks/\w+/images|rats/images|paintings/\w+/blowups) http://www.example.com/boilerplate/sorry.html [R=301,L]
The redirect of course only works if it was a direct request for the image. If it's an <img src= from a browser that doesn't send a referer, all that happens is that the image is not shown.
The problem and the question: This rule will not work as shown, with opening anchor. Leading / has no effect. Wouldn't expect it to, since we're in htaccess, but had to double-check. Omit the opening anchor and all is well. (Detour here as I edit two other rules that I hadn't realized weren't working :()
Further experiment shows that inside this FilesMatch envelope, mod_rewrite --and presumably other mods, though I haven't checked --has lost its aliasing info. The opening anchor
will work if it's used with the full filepath
RewriteRule ^
/home/my-user-name/example.com/rest-of-pattern-here
(The beginning is the format used by my error logs, so that's what I tried. Note leading slash.)
Can someone explain this? Preferably in words of two syllables.
Elsewhere... I started out with two questions, but was able to work out the second answer myself. I post it here because I am utterly certain I will sooner or later forget the answer. And sooner or later I or someone else will have the same question again.
Second question (with answer):
The above quoted rule, with or without anchor and path stuff, will not work at all outside the FilesMatch envelope. Much experimentation
:: boilerplate about why everyone should have a test site and it's definitely worth the $10 for an extra domain name ::
reveals that:
IF
you have a Files or FilesMatch envelope
AND
this envelope contains the "RewriteEngine on" directive (whether or not there are actual RewriteRules)
THEN
RewriteRules outside the envelope for files that match the envelope will not work
UNLESS
the envelope also contains the "RewriteOptions inherit" directive
Yup, the self-same RewriteOptions directive that we were talking about
the week before last [webmasterworld.com]. So it doesn't only apply to nested htaccess files. It also applies to Files(Match) envelopes
within the same htaccess.
Urk.