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!

Vita
  • 173
  • 6

1 Answers1

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:

MrWhite
  • 43,224
  • 4
  • 50
  • 90