title: host-specific .htaccess using if
body: <p>On a service available through different subdomains/domains, I want to be able to use only one .htaccess file. For each possible value of <code>%{HTTP_HOST}</code>, I have set up <code>if</code> blocks like this:</p>
  
  <pre><code>&lt;if "%{HTTP_HOST} == 'staging.project.example.org'"&gt;
      [detailed configuration...]
  &lt;/if&gt;
  
  &lt;if "%{HTTP_HOST} == 'development.project.example.org'"&gt;
      [detailed configuration...]
  &lt;/if&gt;
  </code></pre>
  
  <p>This works well with access configuration lines like:</p>
  
  <pre><code>&lt;if "%{HTTP_HOST} == 'staging.project.example.org'"&gt;
      AuthType Basic
      AuthUserFile ".htusers"
      Require user example
      Order deny,allow
      Deny from all
     Allow from xxx.xx.xx.xxx
  &lt;/if&gt;
  </code></pre>
  
  <p>But <code>RewriteRule</code> lines like this will be ignored by Apache:</p>
  
  <pre><code>&lt;if "%{HTTP_HOST} == 'production.project.example.org'"&gt;
      RewriteEngine On
      RewriteBase /
      RewriteCond %{REQUEST_URI} !^/maintenance\.html$
      RewriteRule ^(.*)$ https://www.project.example/maintenance.html [R=307,P]
  &lt;/if&gt;
  </code></pre>
  
  <p>Is it possible to use <code>RewriteRule</code> with <code>if</code> in .htaccess? I think it might be because of </p>
  
  <blockquote>
    <p>"Only directives that support the directory context can be used within
    this configuration section."<br>
    in <a href="https://httpd.apache.org/docs/2.4/en/mod/core.html#if" rel="nofollow noreferrer">https://httpd.apache.org/docs/2.4/en/mod/core.html#if</a></p>
  </blockquote>
  
  <p>but is there a way around this?</p>