Herding WordPress Cats

A pirate cat looks over a ship.

The Situation

When a web team includes several developers working on various aspects of an enterprise system, the ability for each developer to be able to work with their own local development copy becomes critical. It make work flow less complex and allows for a faster development cycle for building new functions, correcting bugs, and making general improvements.

In WordPress MultiSite (WPMS) and in WordPress 3.x, web developers can choose to enable a sub-domain mode, which creates a sub-site of the main domain name (for example, support.example.com as opposed to example.com/support/). This mode is ideal for using a single instance of WordPress to run a large enterprise system with many site addresses, as it decreases the length of the site name.  However, it adds a wrinkle into the local development work flow process: when working in sub-domain mode locally, WordPress multisite locally causes sub-sites to redirect to the main site domain.

The Workaround

The workaround for this redirect is actually fairly simple:

  • Create a sub-site using WPMS, then
  • Hijack the DNS for that one host to point to your local Apache server.

In our case, each developer in our office has their own ‘local’ subsite – sean-local.wordpress.tld, james-local.wordpress.tld, etc. While each of these was made using our development WPMS install, each developer hijacks their host on their local machines.

The fix requires changes to 3 separate files and restarting the Apache web server, which in our case did not lead to any service disruptions as we are working locally.

File Changes

  • httpd.conf
    (C:/Program Files/Apache2.2/conf/httpd.conf or /etc/apache2/conf/httpd.conf)
  • httpd-vhosts.conf
    (C:/Program Files/Apache2.2/conf/extra/httpd-vhosts.conf or/etc/apache2/conf/extra/httpd-vhosts.conf)
  • hosts
    (C:/windows/system32/drivers/etc/hosts or /etc/hosts)

The location of those files may vary on OSX and Windows, but they should all be there somewhere. The hosts file requires administrative permission to edit.

Edit httpd.conf

  1. First, find and open httpd.conf. Then add the following line to the bottom of the file IF it is already not in the file already:
    Include [path to file]/httpd-vhosts.conf
  2. Replace [path to file] with the path leading to the httpd-vhosts.conf. Make sure to double check that the line is not already in the file. There’s no need to have duplicates.

Edit httpd-vhosts.conf

  1. Now find and open httpd-vhosts.conf, and add the following snippet to the bottom of the file:

    <directory "[path to website directory]">     Allow From All     AllowOverride All </directory> <virtualhost *:80>     ServerName "test.example.com"     DocumentRoot "[path to website directory]" </virtualhost>
  2. The ServerName portion should be the domain of the website that you want to run locally.
  3. Be sure to replace [path to website directory] with the full path to your local installation of wordpress (e.g.C:/Websites/wordpress/).

Edit Hosts File

The last part is the magical part that tells your computer to redirect any outgoing requests to test.example.com back to your machine: the hosts file.  Locate and open the file, and add the following line to the end of the file: test.example.com

Bringing it Live

That should do it. Restart the Apache server, and then load up test.example.com on your browser. You should now be looking at the website that is being served up by your computer, instead of from an external server.

About the Author

Sean Jeng
Web Developer

Sean got his first taste of web development when he saw his friend building an HTML web page to display Dungeons & Dragons character statistics. Since then he has determined that HTML is decidedly not dorky enough, and picked up a couple of programming languages.

Thanks to his stubborn refusal to give up, which is the decades-long result of being really bad at video games, Sean has become proficient at developing and debugging complex solutions. In addition, Sean has learned to be flexible enough to work with various 3rd party software solutions to meet clients’ needs. Just don’t ask him to use OSX. He’s really bad at that too.


John “Jay” Glenn, crowolf, Flickr