Advertisement

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!

Discussion

If you found a mistake in this article or would like to contribute some content to it, please file an issue in this Git Repository

Disclaimer

The contents of this article are put together to the best of the authors' knowledge, but it cannot be guaranteed that it's always accurate in any environment. It is up to the reader to make sure that all information found in this article, does not do any damage to the reader's working environment or wherever this information is applied to. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, arising from, out of or in connection with this article. Please also note the information given on the Imprint' page.