WordPress Blog and Multiple Server Names

I was recently updating my WordPress installation to a freshly released version. My traditional procedure of updating the website is to first make changes to a test version (which is set up on a subdomain of odyniec.net), make sure everything works and looks right, then copy the test files to the public location.

Unfortunately, WordPress didn’t get along with this solution especially well. The WordPress engine didn’t like being accessed through the test subdomain, as it expected a single host name to be designated as its home location. Even though I could open the blog at the test address, all the dynamically generated links to scripts, style sheets, archives and such still pointed to the live version. I also couldn’t log in to the administrative interface at the test version, as it kept redirecting me to the live login page.

I knew PHP stored the actual host name that was used to access the page in the $_SERVER['SERVER_NAME'] variable, and I thought a simple solution would be to make WordPress use that instead of the basic URL set in the configuration. I had very little experience with WordPress internals, so it took me some time to figure out how to implement this, but I eventually succeeded. I wrapped the code into a plugin so that the functionality can be easily enabled or disabled and doesn’t affect the base WordPress code.

So, behold! My first WordPress plugin ever:


Plugin Name:    Server Name Support
Description:    Allows the blog to be accessed with different server names
Author:         Michal Wojciechowski
Version:        0.0.1
Author URI:     http://odyniec.net/

$sns_home = preg_replace('!://[a-z0-9.-]*!', '://' . $_SERVER['SERVER_NAME'],

function sns_home()
    global $sns_home;

    return $sns_home;

function sns_replace_host($url, $path = '')
    return preg_replace('!://[a-z0-9.-]*!', '://' . $_SERVER['SERVER_NAME'],

add_filter('pre_option_home', 'sns_home');
add_filter('pre_option_siteurl', 'sns_home');
add_filter('pre_option_url', 'sns_home');
add_filter('stylesheet_uri', 'sns_replace_host');

add_filter('admin_url', 'sns_replace_host');


In case you’d like to use it (not very likely, as I don’t think my use case is that common), all you need to do is save the above code in a .php file, place it in the wp-content/plugins directory, and activate it in the WordPress admin interface.

8 Responses to “WordPress Blog and Multiple Server Names”

  1. In fact this was exactly what I needed – thanks for posting this. I wanted our WP site to be available via 3 different server names – only one of them worked out of the box; the other two redirected to the WP Admin login page.

    I installed your script as a plugin, activated it – but it made no difference.

    Was there anything else that needed configuration? I’m not very familiar with PHP unfortunately :(

  2. Michal Wojciechowski says:

    Saving the script in the plugins directory and activating it should do the trick. Maybe there is some other problem with your setup? Feel free to contact me by e-mail, I might be able to help you debug it.

  3. [...] the copy of the plugin describe here that I made here and install it: cd ~wpwget http://static.cykod.com/wp/multiple_server_names.phpmv [...]

  4. paul says:

    Hi Michal,

    Can this plugin be used to have multiple urls active at the same time?

    So your test domain and the live url for instance?


  5. Michal Wojciechowski says:

    @Paul: Yes, that’s basically what I use it for.

  6. alex says:

    Perfect. it didn’t work for me until I added this
    add_filter(‘stylesheet_directory_uri’, ‘sns_replace_host’);

    Thank you.

  7. Biranit says:

    You can achieve the same by simply placing these two lines in your wp-config.php:

    define( ‘WP_SITEURL’,’http://'.$_SERVER'SERVER_NAME' );
    define( ‘WP_HOME’,’http://'.$_SERVER'SERVER_NAME' );

  8. Biranit says:

    (the code gets bogged in the comments. I’ll try with SPACES:)

    define( ‘WP_SITEURL’, ‘http://’ . $_SERVER [ 'SERVER_NAME' ] );
    define( ‘WP_HOME’, ‘http://’ . $_SERVER [ 'SERVER_NAME ' ] );

Leave a Reply