Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a dev server and live server, both running my site on Apache. In my .htaccess file, in addition to lots of mod_rewrite commands and other important things, I also have Auth access control for the live server. (The dev server is on my local Mac, so I don't need it there...)

Right now it's hard to sync .htaccess up to the live server since I can't just copy my dev .htaccess file (that would overwrite the Auth commands), so I have to manually edit the live one.

Is there a way to have a conditional in the .htaccess file? I'm imagining something like:

<On Live_Server>
  AuthName "Restricted Area"
  AuthType Basic
  AuthUserFile /home/lmiv/.htpasswds/.htpasswd 
  Require valid-user
share|improve this question
up vote 2 down vote accepted

Very close, yes. You could use an <IfDefine> block for this. It requires that you start Apache with an additional -D directive, the argument to which then becomes available anywhere in the Apache configuration. For instance, if you add -DLive_Server to the command-line options in production, you could then do this in your configuration in all environments:

<IfDefine Live_Server>
  AuthName "Restricted Area"
  AuthType Basic
  AuthUserFile /home/lmiv/.htpasswds/.htpasswd 
  Require valid-user

You could also do the reverse, and define something like -DDev_Server in your development environment, and invert the match:

<IfDefine !Dev_Server>

I prefer the latter, since it requires less "special" configuration for the production environment, and it doesn't require you to restart the production instance just to get the -D added to the command line.

share|improve this answer
Perfect. Thanks. – Eric Mar 8 '12 at 14:33
Is there any way to specify a -D value in the .httpdconf file? – Eric Mar 8 '12 at 18:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.