I was wondering if it was possible to setup a conditional http basic auth requirement based on the virtual host URL in an .htaccess file.
For example what I want to do is have mysite.com and test.mysite.com run off the same code base in the same directory but password protect test.mysite.com. It would be setup this way so that I wouldn’t need to branch my code since my app code can see which vhost/url it’s being served from and pick the database to serve content from.
You can sort of kludge this by using
mod_setenvifalong with themod_authmodules. Use theSetEnvIfNoCasedirective to set which host is password protected. You’ll need a couple of extra directives to satisfy access:Then inside the
Directoryblock (or just out in the open) you have your auth stuff setup, something like this:Now for the require/satisfy stuff:
This will make it so any host that doesn’t match
^test\.mysite\.com\.?(:80)?$will have access without need for auth (Allow from env=!PROTECTED_HOST) but otherwise, we need a valid user (Require valid-user). TheSatisfy anyensures that we just need one of the 2, either the Allow or Require.