is it possible to conditionally include another file in the htaccess file. When i work locally my apache settings are different from my remote server.

Each time i pull in changes through git to the stage site (which shares the same settings as the live server) i have to apply a patch to my htaccess:

diff --git a/.htaccess b/.htaccess
index 4733ffa..1b39d24 100644
--- a/.htaccess
+++ b/.htaccess
@@ -2,6 +2,15 @@
 # Apache/PHP/Drupal settings:

+## uncomment these lines for CGI mode
+## make sure to specify the correct cgi php binary file name
+## it might be /cgi-bin/php-cgi
+  Action php5-cgi /cgi-bin/php5-cgi
+  AddHandler php5-cgi .php

Is there a way to conditionally include this into my htaccess file. Thus, i could just gitignore this particular include file.

I could do a git hook but i'm not well versed and not interested in investing time at this moment to learn that.

P.S. if you're curious what my patch does is it provides the stage server directives as to where to run php as cgi.


Apache allows <IfDefine> and <IfModule> directives within .htaccess files.

If you want to use <IfDefine> then Apache needs to be started as httpd -Dsomedefinition. See here.

<IfModule> is useful if you only want certain directives to be enabled if a given Apache module is loaded (e.g. if mod_perl has been loaded). See here.

  • Excellent suggestion. However, much will depend on how these two git repos are set up, and which parts of the config are kept in there and synchronized. – adaptr Feb 15 '12 at 15:25
  • I'm guessing that the Production site is using CGI mode but the development is some built-in server like WEBrick, therefore the Production server could be started as httpd -DUseCgiMode. I agree that it's dependent on how much is kept in sync, though. – James O'Gorman Feb 15 '12 at 15:27
  • He indicated that the CGI stuff was for a staging server, and differs from production - which makes me expect production to use mod_php. – adaptr Feb 15 '12 at 15:29
  • 1
    Ah yes, I misread that slightly. In which case <IfModule> would probably work best. – James O'Gorman Feb 15 '12 at 15:32
  • Not at all - IfDefine is still a very good solution, since he can simply pass -DStaging on the staging server and check for that. HOWEVER, as my answer already suggests, thses settings DO NOT BELONG in htaccess. ever. – adaptr Feb 15 '12 at 15:33

.htaccess does not support the use of the Include directive, period.

What you should do instead is define the php action in httpd.conf on the staging server.

This will be a global setting if it is not overridden anywhere downstream.

  • In fact, if you can edit the global settings, you should not be using .htaccess at all. .htaccess is really only meant for users that do not have a way to modify their own server config. – stew Feb 15 '12 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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