Using .htaccess To Make Testing Easier


Do you have trouble implementing a phase II of a website? I know it can be a pain having to develop a phase II on top of an existing live site. Obviously you can’t just modify the live server without testing; but other alternatives can be troublesome as well.

Its not fun having 2-3 different copies of a website. Get’s confusing which is live, what changes have been done on which, etc. Well I found one way to somewhat ease the pain. I know some of you out there have more in depth solutions such as ZooKeeper or Capistrano, but both didn’t really suit my needs and are a bit complex.

The Folder Structure

My solution would be to have two folders in the public_html of your website. Have one named “staging” and the other named “production”.


Each folder would be a “copy” of your site, and each would have its own db. I would suggest you name your db’s yoursite_staging, and yoursite_production respectively.

The .htaccess

The only real requirement for this is that must have a unix server as that is the only server that supports .htaccess. You also have the mod_rewrite module installed.

Once you are sure the requirements are met, put the following in your .htaccess:

# enable module_rewrite
RewriteEngine On

# make sure the host is what you expect, and don't rewrite subdomains
RewriteCond %{HTTP_HOST} ^(www.)?$

# make sure they aren't requesting the actual production directory.  Otherwise it would be a continuous loop.
RewriteCond %{REQUEST_URI} !^/production/

# rewrite any request file to the production directory
RewriteRule ^(.*)$ /production/$1

The following are some examples of how the above will redirect:

/index.php =>  /production/index.php
/css/styles.css?v=1  =>  /production/css/styles.css?v=1
/images/gallery/myimage.jpg => /production/images/gallery/myimage.jpg

Switching Folders

Now if you ever need to change the folder that is shown live on your site, just change any line that has “production” in it. Alternatively you could just rename the “production” folder to “staging” and vice versa.

Your Thoughts?

What do you think of this concept? Is it too simple? If you got a better way to do something like this please sure it in the comments!

Be Sociable, Share!

Written by brenelz

Hello everyone, I'm Brenley Dueck or better known as Brenelz. I currently run my own business called Brenelz Web Solutions which focuses primary on winnipeg website design. The web technologies I most specialize in are CSS, jQuery, AJAX, PHP, and the MySQL database. Please make sure to follow me on twitter.


One Response to “Using .htaccess To Make Testing Easier”

  1. Michelle Mendoza Says:

    June 3rd, 2011 at 2:56 pm

    Thanks very much for the information! It does seem simple, but sometimes simple is better in web design.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

connect with me!