Categories
WordPress

Multi-Domain WordPress with Dynamic Table Prefixes

WordPress typically requires separate installations for different domains or subdomains.

Here I present a method to serve multiple domains or subdomains from a single WordPress instance using dynamic table prefixes.

This method is simpler and more flexible than WordPress Multisite for hosting multiple domains from one installation.

But keep in mind, due to shared themes and plugins (read below), it's best suited for personal or intra-organizational projects rather than hosting multiple client sites.

Implementation

To implement this solution, modify the wp-config.php file in your WordPress installation:

  1. Open wp-config.php in the main WordPress directory.
  2. Locate the line defining the table prefix:
    $table_prefix = 'wp_';
  3. Replace it with:
    $arrPrefix = explode( ".", $_SERVER["HTTP_HOST"] );
    $table_prefix = implode( "_", $arrPrefix ).'_';
    $table_prefix = str_replace("-", "_", $table_prefix);

This code:

  • Splits the hostname into an array based on periods
  • Joins array elements with underscores for a unique prefix
  • Replaces hyphens with underscores for database compatibility

How It Works

The modified wp-config.php creates a unique table prefix for each domain:

  • example.com becomes example_com_
  • subdomain.example.com becomes subdomain_example_com_
  • my-site.com becomes my_site_com_

This allows each domain to use its own set of database tables while sharing WordPress core files, plugins, and themes.

Server Configuration

Ensure all domains point to the same WordPress directory.

For Apache:

ServerName domain.tld
ServerAlias *.domain.tld
DocumentRoot /path/to/wordpress

For Nginx:

server {
    listen 80;
    server_name domain.tld *.domain.tld;
    root /path/to/wordpress;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

Setting Up New Domains

  1. Point the new domain to your server.
  2. Run the standard WordPress installation process for each new domain.
  3. WordPress will automatically create the necessary tables with the correct prefix.

Shared Resources and Considerations

  1. Plugin and Theme Directories:

    • All installations share the same plugin and theme directories.
    • Installing a plugin or theme in one installation makes it available to all.
    • Activation is separate for each installation. Activating a plugin or theme in one does not activate it in others.
  2. Uploads Directory:

    • By default, all installations share the same uploads directory.
    • To separate uploads for each installation, consider setting a custom upload directory:

    Add this to your wp-config.php:

    define( 'UPLOADS', 'wp-content/uploads/' . $table_prefix );

    This creates separate upload directories based on the table prefix.

  3. Migration Considerations:

    • If you plan to migrate installations off this setup in the future, having separate upload directories will simplify the process.

Leave a Reply

Your email address will not be published. Required fields are marked *