Given you have two different customers which need to access the same resource. The URL should be the same for both, e.g. “https://www.example.org/resource.db”, but the response should be different.
From
“2.4” on the apache httpd
can make use of its
expressions in its rewrite engine. There’s an expression -R
which does
the “same as %{REMOTE_ADDR} -ipmatch ...
, but more efficient” which matches
the IP address of a requesting client to an IP network.
To redirect requests internally use the following in your apache configuration.
Besides expressions, this configuration uses one of httpd
’s “Rewrite Rule
Flags”:
END
. This flag prevents any subsequent rewrite processing if the
rule matches.
<VirtualHost 10.0.0.1:80>
ServerName www.example.org
ServerAlias www
UseCanonicalName Off
ServerAdmin admin@example.org
DocumentRoot /srv/www/
<Directory "/srv/www/">
AllowOverride None
Require all granted
</Directory>
RewriteCond expr "-R '10.0.1.0/24'"
RewriteRule ^/resource.db$ /customer1/resource.db [END]
RewriteCond expr "-R '10.0.2.0/24'"
RewriteRule ^/resource.db$ /customer2/resource.db [END]
RewriteRule ^/resource.db$ /resource.db [END]
</VirtualHost>
The most interesting part is this. In this code snippet, the webserver checks for the client IP address and redirects the request internally if the client IP is in the given IP range and stops checking for other rewrite rules. The last line is a default redirect rule which always matches.
RewriteCond expr "-R '10.0.1.0/24'"
RewriteRule ^/resource.db$ /customer1/resource.db [END]
RewriteCond expr "-R '10.0.2.0/24'"
RewriteRule ^/resource.db$ /customer2/resource.db [END]
RewriteRule ^/resource.db$ /resource.db [END]
Try it yourself! Happy Redirecting! Thanks for reading!