I'm trying to configure Apache to redirect users to different paths based on their authentication credentials (using htpasswd). I've managed to set up ProxyPass, which enables the redirection of the request to a custom path, however all of the users now get redirected to the same path. This happens because of using the same <Location />
directive for all of the users. When I try with <Location /user123>
, <Location /user456>
,.. it works like intended, but I'd like for Apache to forward the /
location to appropriate paths based on provided credentials. Is this possible?

- 1
- 1
-
Could be possible by using rewriterule instead of ProxyPass, it supports variables. – Gerald Schneider Dec 18 '22 at 06:23
2 Answers
Looking at apache with mod_rewrite. I believe you could do something like the following. The remote_user variable is available to apache in the config. I believe you can do something like the following.
RewriteCond "%{REMOTE_USER}" "!^$"
RewriteRule "^/(.*)" "/$1/%{REMOTE_USER}" [R]
The condition says if the remote user exists with a value that is not an empty string then rewrite urls with a remote url. You also could do a redirectmatch as well.
RedirectMatch "^/(.*)" "/$1/%{REMOTE_USER}"
Here are my references. This shows the variables available to mod_rewrite https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
This show examples of mod_rewrite in action https://httpd.apache.org/docs/2.4/rewrite/remapping.html

- 36
- 2
-
That's pretty much what I imagined. But the OP want's to proxy, not redirect. – Gerald Schneider Dec 20 '22 at 06:06
-
Really not sure about the proxy. I would put these rewrite rules on whatever is on the receiving end of the proxy. – schezel2000 Dec 22 '22 at 04:58
Looking at the question more closely, since you are using the apache instance to authenticate using htpasswd file. I believe you can reference the REMOTE_USER variable.
I believe you can use something like the following in your apache config.
ProxyPass / http://localhost:8080/%{REMOTE_USER}/
ProxyPassReverse / http://localhost:8080/%{REMOTE_USER}/

- 36
- 2
-
And if ProxyPass doesn't support variable expansion ; use the RewriteRule from your other answer, but with a [`P|proxy`](https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_p) flag rather than the R|redirect . – diya Dec 22 '22 at 08:24
-