2

It's likely that I missed the answer already because I'm not even sure how to phrase this, but here it goes...

I'm attempting to create a healthcheck virtual--something "file", maybe, so whenever the address ends in something like .../check it is served some .html file from a specific location at the server-level config file so it's matched regardless of virtual host or if whatever is in the patch before /check -- in this example -- exists or not. e.g;

https://dns/non-existentPath/moreRandom/check → /var/www/html/someFile.html
http://dns/check → /var/www/html/someFile.html
http://dns/virtualHostThatExists/check → /var/www/html/someFile.html

I managed doing it for this pseudo-universal assets directory that is matched at any level so files used in things like error pages and other domain-wide resources are always in the same place, it uses the AliasMatch directive:

AliasMatch "(.*)/specialDirExample/([^.].*)" "/var/www/html/someLocation/domainWide/assets/$2"

it works perfectly. But in the /check case since I'm not trying to serve a directory but a file, as far as I know a rewrite rule, not AliasMatch is what I need. I started reading the documentation I understand how to rewrite things but to a certain point only, I haven't been able to make up how the "any level" part comes in, if maybe one of the alias directives should be mixed in too. Right on the newbie docs redirection is mentioned too, making it a little harder to follow.

Could you give an example of how this would be done please? I could solve this very easily with a reverse proxy but this is for the proxy!

1 Answer 1

2

You can do this using mod_rewrite in the main server config (outside of any <VirtualHost> container):

RewriteEngine On

RewriteRule /check$ /var/www/html/someFile.html [L]

Any URL-path that ends with /check (including example.com/check) is internally rewritten to the file-path /var/www/html/someFile.html.

This is processed before any Alias (or AliasMatch) directives.

However, if you also have mod_rewrite directives directly in any child <VirtualHost> containers (not in <Directory> containers - which works in a different (directory) context) then you also need to enable mod_rewrite inheritance at the server/vHost level, otherwise, the mod_rewrite directives in the vHost (child) will completely override the directives in the server (parent) context (and the above rule would never be processed).

For example, in the main server config, immediately after the above RewriteEngine directive:

RewriteOptions InheritDownBefore

This will process the mod_rewrite directives in the server config before any mod_rewrite directives in any child vHost containers. (Requires Apache 2.4.8+)

Reference:

2
  • Thank you! I thought RewriteCondition was needed then not, then I thought I had it for a minute but the whole directory-vs-location situation came up and I just had to come for help. I'm comparing a working Debian 11 setup versus this one (RHEL 8.5) but I'm only getting started. I'll check myself into a hotel with an iPad to focus while I read the docs this weekend. But for the time being you saved me--thanks! If you're still feeling generous: what's the diff between L/END if $ meaning end and .html [loosely] meaning file so also "that's it=the end" are there?
    – Vita
    Commented Jan 6, 2022 at 18:38
  • @Vita I don't see a need for an additional condition (RewriteCond directive) here. In a server context (as we are dealing with here) there is really no difference between L (last) and END. Both terminate the rewrite engine. I'm not sure what you mean with regards to the comparison with $ (and .html)? The two are unrelated. Where L and END differ are in a directory context (eg. .htaccess). L terminates the current pass only, whereas END terminates the rewrite engine completely. (Incidentally, END was new with Apache 2.4 as a convenience.)
    – MrWhite
    Commented Jan 6, 2022 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.