Different settings for dev and live in your htaccess

10th August 2013

A couple months ago I set myself the challenge of finding a method that would allow for a solid way of checking whether or not the current server was live or production.

I wanted the method to be as independent as possible and I wanted to be able to use it in my code and in my .htaccess

The method includes changing a file on your development server called /etc/apache2/envvars or /etc/sysconfig/httpd, that's it... I promise! This is particularly useful when you are running a development server (perhaps with dynamic vhosts) and want certain RewriteRules to not be applied in your development environment.


Open /etc/apache2/envvars on your development server in your favorite editor and add the following line to the bottom of the file and restart apache:

export APACHE_ARGUMENTS="-D dev"


Open /etc/sysconfig/httpd on your development server in your favorite editor and add or change the following line and restart httpd:

OPTIONS="-D dev"

Now you can use any of the following methods to determine whether you are on your development server or live server.


<IfDefine !dev>
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The above checks if it's not the development server and then forces HTTPS.


Add this to your .htaccess as well to get the PHP method underneath to work on CentOS

<IfDefine dev>
    SetEnv APACHE_ARGUMENTS "-D dev"

PHP (through apache)

define('DEV', stristr(getenv('APACHE_ARGUMENTS'), '-D dev') !== false);

I've made a ZF2 module to do the above AND allow for a dev.php config file to be loaded, overwriting all other settings when on development server.


And there you go, if you have any suggestions or perhaps different languages to be included in this list feel free to comment, tweet or send me an email.

comments powered by Disqus