<Directory /var/www/html/>
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /etc/httpd/conf/tag.pwd
Require valid-user
RewriteRule ^/(.*) http://xxxxxx:xxx/$1 [P,L]
</Directory>
There are 2 issues here that will prevent your RewriteRule
from doing anything:
You need to enable the rewrite engine inside the <Directory>
container (a directory context). You've (incorrectly) enabled the rewrite engine in the outer <VirtualHost>
container (a virtualhost context) - in which you don't have any mod_rewrite directives. The <VirtualHost>
and <Directory>
containers work in different contexts. If you don't enable the rewrite engine inside the <Directory>
container then the directives will simply be ignored.
RewriteEngine On
When used in a directory context (<Directory>
and .htaccess
) the URL-path matched by the RewriteRule
pattern does not start with a slash, since the directory-prefix (that ends in a slash) has been removed. So, you need to remove the slash prefix from the regex, otherwise, it will simply never match in a directory context:
RewriteRule (.*) http://xxxxxx:xxx/$1 [P,L]
(The ^
prefix on the pattern then becomes superfluous.)
Summary
Actioning the above points, this becomes:
<Directory /var/www/html/>
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /etc/httpd/conf/tag.pwd
Require valid-user
RewriteEngine On
RewriteRule (.*) http://xxxxxx:xxx/$1 [P,L]
</Directory>
Alternatively, you move the RewriteRule
directive outside of the <Directory>
container and use this directly inside the <VirtualHost>
container in which you've already enabled the rewrite engine.
However, in this context, the mod_rewrite directives will execute before the authorisation directives inside the <Directory>
container, so you will need the additional condition that checks the REMOTE_USER
via a look-ahead (ie. LA-U:REMOTE_USER
), as mentioned in the other answers.