Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>So is this pie-in-the-sky wishful thinking, or can it be done?</p> </blockquote> <p>IMO, yes. You're never going to be able to get predictable "scoping" of rules based on ENV variables or anything like that. There doesn't exist arbitrary <code>if(something) { do everything in here }</code> in apache. Lots of directives won't work inside certain scopes, and in later on, when you need to change how something works, you're more likely to break what you have than simply amending it.</p> <p>The best way is to <a href="http://httpd.apache.org/docs/2.2/howto/htaccess.html#related" rel="noreferrer">not use htaccess files at all</a>:</p> <blockquote> <p>You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a <code>Directory</code> block, as it will have the same effect with better performance.</p> </blockquote> <p>Create a separate vhost for local, dev, and production. Turn them on or off as needed, whatever global config they share, store that elsewhere (like in a file called <code>global.includes</code>) and then use the <code>Include</code> directive in all 3 vhosts. If you need to apply rules to specific directories, use the <code>&lt;Directory&gt;</code> block instead of htaccess files.</p> <p>If you'd rather stick everything inside htaccess files, you <em>could</em> try putting everything <a href="http://httpd.apache.org/docs/2.2/mod/core.html#ifdefine" rel="noreferrer">in <code>&lt;IfDefine&gt;</code> blocks</a>, it's probably the closest thing you'll have to your pseudo-code in your question. Essentially something like:</p> <pre><code># Global htaccess rules RewriteEngine On RewriteRule ^foo$ /bar [L] # Only local &lt;IfDefine LocalInstance&gt; RewriteRule ^local/foo /bar [L] &lt;/IfDefine&gt; # Only dev &lt;IfDefine DevInstance&gt; RewriteRule ^dev/foo /bar [L] &lt;/IfDefine&gt; # Only production &lt;IfDefine ProductionInstance&gt; RewriteRule ^dev/foo /bar [L] &lt;/IfDefine&gt; </code></pre> <p>Then when you start apache, you'd need to pass in <code>-DLocalInstance</code>, <code>-DDevInstance</code>, or <code>-DProductionInstance</code> as command line paramaeters or using the <code>Define</code> directive (with only one argument) somewhere in your vhost config. This isn't guaranteed to work as smoothly as it looks like it should, I've ran into unexplained issues with <code>&lt;IfDefine&gt;</code> before, especially if you try to get too fancy.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload