WordPress主域名与子域名共用同一数据库解决方案咨询
Hey there! Let’s figure out how to get your main domain (https://www.pearltutorial.com) and subdomain (http://besttutorial.pearltutorial.com) sharing the same database—since Multisite didn’t work for you, let’s walk through actionable fixes for Multisite first, then cover alternative approaches if you still hit roadblocks.
Multisite is the official WordPress way to run multiple sites on one database, so let’s rule out common misconfigurations first:
Double-check your wp-config.php settings
Make sure you’ve added the correct Multisite constants to your wp-config.php (right above the/* That's all, stop editing! Happy publishing. */line). Here’s the exact setup you need for subdomain Multisite:/* Multisite Configuration */ define( 'WP_ALLOW_MULTISITE', true ); define( 'MULTISITE', true ); define( 'SUBDOMAIN_INSTALL', true ); define( 'DOMAIN_CURRENT_SITE', 'www.pearltutorial.com' ); define( 'PATH_CURRENT_SITE', '/' ); define( 'SITE_ID_CURRENT_SITE', 1 ); define( 'BLOG_ID_CURRENT_SITE', 1 );If you initially set up Multisite with a non-www domain, that could break subdomain routing—stick with the exact main domain you’re using.
Verify server & DNS setup
- Your subdomain
besttutorial.pearltutorial.commust point to the same server IP as your main domain. Check your DNS records for an A record or CNAME pointing correctly. - For Apache: Update your virtual host config to include a
ServerAlias *.pearltutorial.comso all subdomains are routed to your WordPress installation. - For Nginx: Set your
server_nametowww.pearltutorial.com besttutorial.pearltutorial.com(or use a wildcard*.pearltutorial.com). - Don’t forget SSL! Your subdomain needs a valid SSL certificate (a wildcard cert works great for all subdomains) to avoid mixed-content errors.
- Your subdomain
Update .htaccess (for Apache) or Nginx config rules
When you enable Multisite, WordPress gives you custom rewrite rules to add to your .htaccess (or Nginx config). If you skipped this step, Multisite won’t route traffic correctly. For Apache, the rules look something like this:RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase / RewriteRule ^index\.php$ - [L] # add a trailing slash to /wp-admin RewriteRule ^wp-admin$ wp-admin/ [R=301,L] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] RewriteRule ^(wp-(content|admin|includes).*) $1 [L] RewriteRule ^(.*\.php)$ $1 [L] RewriteRule . index.php [L]
If Multisite still won’t cooperate, you can have two separate WordPress installations share the same database—here are two ways to do it:
Option 1: Full Database Sync (Identical Sites)
This makes both domains mirror each other completely—any change to one site (posts, plugins, settings) affects the other.
- Match wp-config.php settings
For both your main domain and subdomain WordPress installs, set the same database credentials and table prefix in wp-config.php:
Important: Make sure both WordPress installs are on the same version to avoid table structure conflicts.// Same for both sites define( 'DB_NAME', 'your_database_name' ); define( 'DB_USER', 'your_db_user' ); define( 'DB_PASSWORD', 'your_db_password' ); define( 'DB_HOST', 'localhost' ); $table_prefix = 'wp_'; // Use the same prefix for both
Option 2: Partial Content Sharing (Custom Control)
If you only want to share specific content (like posts, users, or products) instead of everything, you can keep separate table prefixes but query the main site’s database from the subdomain.
- Add a secondary database connection
In your subdomain’s wp-config.php, add a new connection to your main site’s database (use the same credentials as before):// Connect to main site's database $main_site_db = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST ); - Query content from the main site
In your subdomain’s theme or plugin, use the$main_site_dbobject to pull content. For example, to display the 5 latest posts from your main site:
You can call this function anywhere in your subdomain’s theme (like infunction display_main_site_posts() { global $main_site_db; $posts = $main_site_db->get_results( "SELECT ID, post_title FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC LIMIT 5" ); if ( $posts ) { echo '<ul>'; foreach ( $posts as $post ) { echo '<li>' . esc_html( $post->post_title ) . '</li>'; } echo '</ul>'; } }front-page.phporsidebar.php).
- Always back up your database before making any changes—especially when messing with Multisite or shared database setups.
- If you go the direct database share route, keep plugins/themes aligned across both sites to avoid compatibility issues.
内容的提问来源于stack exchange,提问作者Khalid

