Given this folder / file structure:
private/.htpasswd
public/.htaccess
... where public
is the root folder of a virtual host in Apache, and private
is its sibling folder:
How do I define a relative path for AuthUserFile
in the .htaccess
file, such that it is able to access /private/.htpasswd
I've tried:
AuthType Basic
AuthUserFile ../private/.htpasswd
require valid-user
But that doesn't work, because it tries to find the file relative to the ServerRoot, in stead of relative the virtual host root.
I won't have access to the config file on the production server (shared host) and I don't want to define an absolute path, because my testing and production file system structures don't match.
Is it still possible to achieve what I want, given these conditions?
You may use symlink on testing system, for example
/srv/www/vhost/private/.htpasswd -> /var/www/vhost/private/.htpasswd
. The first path need to be same as on production server. Then you will be able to use the same paths on both servers.Options FollowSymLinks
may be required for this. I don't see any way to include config files not relatively to ServerRoot.If the symlink solution posted by Selivanov Pavel does not work, you could disable Auth* directives in .htaccess using
ALlowOverride -AuthConfig
(or disable .htaccess altogether) and move the Auth config to the Apache conf on your local machine only.Let's take an example.
Your application is located in /var/www/myApp on some Linux server
.htaccess : /var/www/myApp/.htaccess
htpasswdApp : /var/www/myApp/htpasswdApp. (You're free to use any name for .htpasswd file)
To use relative path in .htaccess:
But it will search for file in server_root directory. Not in document_root.
In out case, when application is located at /var/www/myApp :
document_root is /var/www/myApp
server_root is /etc/apache2 //(just in our example, because of we using the linux server)
You can redefine it in your apache configuration file ( /etc/apache2/apache2.conf), but I guess it's a bad idea.
So to use relative file path in your /var/www/myApp/.htaccess you should define the password's file in your server_root.
I prefer to do it by follow command:
You're free to copy my command, use a hard link instead of symbol,or copy a file to your server_root.
Without a leading
/
then the path provided toAuthUserFile
is relative toServerRoot
. Your private directory will have a path relative to ServerRoot so just provide that - givenand a provate directory at
then
should work.