2

I'm defining a RewriteMap in my Apache httpd configuration. (This is working correctly.)

RewriteMap redirects-list txt:/path/to/redirects.txt

But when the map file (/path/to/redirects.txt) doesn't exist, the server throws an error:

RewriteMap: file for map redirects-list not found:/path/to/redirects.txt

I want to make this fault-tolerant, where the RewriteMap is set if and only if the file exists.

(For example, if it doesn't exist, maybe there's a way to automatically use a fallback, a blank file like /dev/null.)

I'd appreciate any help.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
Anirvan
  • 401
  • 1
  • 3
  • 13
  • 2
    Under what circumstances can the map file not exist? Bear in mind that any changes to the state of the map file can only be detected when the webserver restarts. – MrWhite Jan 30 '19 at 08:01
  • You presumably have other directives that call this rewrite map and are therefore dependent? – MrWhite Jan 30 '19 at 09:32
  • I'm looking at a situation where there's a tiny chance that the RewriteMap file may not exist. As it stands, the server sees this missing file as a fatal error that prevents it from starting/restarting. I'd like to find a way to avoid this. Thanks! – Anirvan Jan 30 '19 at 20:50
  • 2
    You should always run `apachectl configtest` (after changing your config) before you start the server. If the command fails and you see an error containing something like "RewriteMap: file for map xxx not found:/path/to/my_rewritemap.txt" you create the missing file (`touch /path/to/my_rewritemap.txt`) and run `apachectl configtest` again and fix the next error until the command is successful. Then you start the server. – Freddy Jan 30 '19 at 21:36

2 Answers2

2

Use a real empty file or a file with comments. Changes to the file will be recognized by Apache without the need to reload/restart.

https://httpd.apache.org/docs/2.4/rewrite/rewritemap.html#txt

Cached lookups

The looked-up keys are cached by httpd until the mtime (modified time) of the mapfile changes, or the httpd server is restarted. This ensures better performance on maps that are called by many requests.

A missing RewriteMap file is an error (apachectl configtest) and /dev/null as file works, but is bad style. In this case it might be better to comment those lines in your config.

Freddy
  • 1,999
  • 5
  • 12
  • I was asking how to ensure that RewriteMap is set if and only if the file exists. "Just create the file if it doesn't exist" doesn't specifically answer that question. – Anirvan Jan 30 '19 at 20:52
2

There doesn't seem to be a way to set a RewriteMap conditionally. I tried setting the RewriteMap in an <If> directive using an Apache expression to check the existence of the file. For example:

<If "-f '/path/to/redirects.txt'">
    RewriteMap redirects-list txt:/path/to/redirects.txt
</If>

However, this is syntactically invalid and the server will fail to start with error:

RewriteMap not allowed here

As stated already, you should test the config before (re)starting the Apache server.


It's worth noting that if there are dependent directives that use this (non-existent) RewriteMap then these will simply fail silently. (Even with LogLevel rewrite:trace6 there doesn't appear to be anything meaningful logged in this regard?). So, having the server fail to start when the map file does not exist would seem to be the better option (even though removing the map file later does not result in an "error").

MrWhite
  • 11,643
  • 4
  • 25
  • 40