Easy Site Switching for Web Developers
Posted on July 20th, 2005
The other day I realized how inefficient it can be to switch between public and private versions of a web site you're working on. For example, let's say you're making a change to www.example.com and your development area is dev.example.com. Your web browser starts off on dev.example.com/path/to/file, and you do whatever it is you're going to do. Then you upload to www.example.com. Naturally you'll want to double check your work to make sure everything is kosher, so you have to click into the address bar of your browser and arrow or mouse your way over to the "dev" and replace it with "www". Not quite click-and-go, but not horribly inconvenient either.
Actually, it is inconvenient. If your work takes you to several pages of a site or if you notice something wrong on the www that needs to be fixed on dev, you can end up doing a lot of URL fiddling over the course of a day. Maybe you have separate browser tabs open, and you switch between them as needed. Slightly more convenient, yes, but redundant all the same.
My way of getting around all that is with a utility I call URL Boomerang. It's a bookmarklet and a PHP script that put an end to URL fiddling if all of the following are true:
- You're doing an ordinary GET request.
- Your public and private sites have distinct domain names, such as "dev" and "www".
The PHP script is very simple. The first thing you do with it is populate an array with all the domain pairs that you need to switch between. For the scenario described above, that would be:
$domains[] = array('http://dev.example.com,'http://www.example.com');
Load the script in a browser. The single link that is displayed should be dragged to the bookmark toolbar of your browser. When clicked, whatever URL you're currently viewing will be passed to the PHP script. The PHP script will then consult its domain array and see if it can find a match. If it does, it'll redirect you to the next domain in the sequence and preserve any file path. You end up with a one click method of flipping between two or more versions of a site.
If your environment is a little more formal, and you have a staging area in addition to development and live areas, this will still work since the array is always treated as an ordered sequence.
What's nice about this setup is that you can use it for multiple sites; the $domains array is an array of arrays.
Interested? Here's the PHP script as a TEXT file. Download it, populate the $domains array, and let me know if you find it useful.