1Sử dụng Wildcard domain cho NHIỀU SITE wordpress Cấu hình trên xampp hiện tại là:

Sử dụng Wildcard domain cho NHIỀU SITE wordpress Cấu hình trên xampp hiện tại là: 
ServerName wp1.com ServerAlias *.wp1.com 
DocumentRoot "C:/xampp82/htdocs/s1wp1" 
SSLEngine on SSLCertificateFile "conf/mwp1/ssl.crt/server.crt" 
SSLCertificateKeyFile "conf/mwp1/ssl.key/server.key" 
AllowOverride All 
Require all granted 
TÔi muốn nó chạy wp1.com và sub1.wp1.com và sub2.wp1.com tôi phải cấu hình như nào cho đúng. Tôi đang sử dụng xapp trên windows

Và đã cấu hình thành công

wp-config.php

<?php
/**
 * The base configuration for WordPress
 *
 * The wp-config.php creation script uses this file during the installation.
 * You don't have to use the website, you can copy this file to "wp-config.php"
 * and fill in the values.
 *
 * This file contains the following configurations:
 *
 * * Database settings
 * * Secret keys
 * * Database table prefix
 * * ABSPATH
 *
 * @link https://developer.wordpress.org/advanced-administration/wordpress/wp-config/
 *
 * @package WordPress
 */
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wp1' );
/** Database username */
define( 'DB_USER', 'root' );
/** Database password */
define( 'DB_PASSWORD', '' );
/** Database hostname */
define( 'DB_HOST', 'localhost' );
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
/**#@+
 * Authentication unique keys and salts.
 *
 * Change these to different unique phrases! You can generate these using
 * the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}.
 *
 * You can change these at any point in time to invalidate all existing cookies.
 * This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define( 'AUTH_KEY',         '!b>mMQ90aj-pu0.:3k:6d}),^*!A!Q[@qB:Dboci +mG]a:=c(BIZ{B/0OFd(`J=' );
define( 'SECURE_AUTH_KEY',  'Y2yho<.qJz`+:%!al+JG=s`vd=uH5,8i|f>#@.xI_,;< Vr[>dah_:Jk@?{Lvw]!' );
define( 'LOGGED_IN_KEY',    '*opK(8 ZDVK5^EVMiTGy$@5K6>>0`&u0j4JbTFJYx`7D)gg-3/zo^>I!^HGy)R}=' );
define( 'NONCE_KEY',        '4*U0[s$#ot8U0] s^6n7(M-MnLv.sI`j$cF3^GZ1PWZn[tUrvf~&gTV*{hOl8EPU' );
define( 'AUTH_SALT',        '6^<m$~{QDVAU*cn4/sC#B4;cMa{EUpN4W.9GNVJA+W_A|lC)I/`.4;xM>jHKOKw#' );
define( 'SECURE_AUTH_SALT', '3K^4%x_}#ob5zB.^^>0~R{y>$,%Xkyd{a>k#(|Ax.]d-:BJ3Iu-KHk>@xt}r:l7a' );
define( 'LOGGED_IN_SALT',   'IA;@LY%o2a:Z+{J{jsD@_2xtkh,37Hl4eO0,NIl8v6aXy+`5_(OpPo)3.tFfz}CA' );
define( 'NONCE_SALT',       'YtvbrnrY9pS9z`_nJ~u#K8Q0 _ZaQ3!X$q90O<-fNPN].aGV-PhH4}(J/*]U1z6X' );
/**#@-*/
/**
 * WordPress database table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 *
 * At the installation time, database tables are created with the specified prefix.
 * Changing this value after WordPress is installed will make your site think
 * it has not been installed.
 *
 * @link https://developer.wordpress.org/advanced-administration/wordpress/wp-config/#table-prefix
 */
$table_prefix = 'wp_';
/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the documentation.
 *
 * @link https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/
 */
define( 'WP_DEBUG', false );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
define( 'MULTISITE', true );
define( 'SUBDOMAIN_INSTALL', true );
define( 'DOMAIN_CURRENT_SITE', 'wp1.com' );
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );
// define( 'WP_DEBUG', true );
// define( 'WP_DEBUG_DISPLAY', true );
// @ini_set( 'display_errors', 1 );
/* Add any custom values between this line and the "stop editing" line. */
/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
	define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';

.htaccess

C:\xampp82\apache\conf\extra\httpd-vhosts.conf

Cấu hình hiện tại của bạn với ServerName wp1.com và ServerAlias *.wp1.com là đã đúng để xử lý domain chính wp1.com và tất cả các subdomain như sub1.wp1.com, sub2.wp1.com, v.v., trỏ cùng về một thư mục DocumentRoot ("C:/xampp82/htdocs/s1wp1").

Nếu bạn đang setup WordPress Multisite với chế độ subdomain (một site chính + nhiều site con trên subdomain), thì cấu hình này là phù hợp, vì tất cả subdomain sẽ dùng chung một installation WordPress, và WordPress sẽ tự handle routing dựa trên hostname.

Các bước kiểm tra và hoàn thiện để nó chạy đúng

  1. Chỉnh sửa file hosts trên Windows (đây là phần quan trọng nhất, vì Windows hosts file không hỗ trợ wildcard* trực tiếp):

    • Mở file: C:\Windows\System32\drivers\etc\hosts bằng Notepad với quyền Administrator.

    • Thêm dòng:text

      (Thêm bao nhiêu subdomain bạn muốn test. Mỗi lần thêm subdomain mới, phải thêm dòng mới vào đây).

    • Lưu file và flush DNS: Mở Command Prompt (Admin) chạy lệnh ipconfig /flushdns.

  2. Cấu hình Virtual Host trong XAMPP (bạn đã làm gần đúng):

    • File cần chỉnh: C:/xampp82/apache/conf/extra/httpd-vhosts.conf

    • Giữ nguyên khối <VirtualHost *:80> (cho HTTP) như bạn có, nhưng đảm bảo đầy đủ:

      text

    • Nếu bạn muốn HTTPS (vì bạn đã có SSLEngine on):

      text

      Lưu ý về SSL: Cert tự ký của bạn chỉ match chính xác wp1.com (CN=wp1.com). Khi truy cập subdomain như sub1.wp1.com qua HTTPS, browser sẽ báo lỗi certificate mismatch. Để tránh:

      • Tạo cert wildcard (*.wp1.com) bằng OpenSSL.

      • Hoặc test tạm bằng HTTP trước.

      • Hoặc dùng tool như mkcert để tạo cert local trusted.

  3. Đảm bảo Apache nhận Virtual Host:

    • Trong file C:/xampp82/apache/conf/httpd.conf, tìm dòng Include conf/extra/httpd-vhosts.conf phải không bị comment (không có # đầu dòng).

  4. Restart Apache sau mọi thay đổi.

  5. Nếu là WordPress Multisite:

    • Trong wp-config.php: define('WP_ALLOW_MULTISITE', true); rồi setup network với subdomain.

    • define('DOMAIN_CURRENT_SITE', 'wp1.com');

    • WordPress sẽ tự handle các subdomain khác.

Nếu bạn muốn NHIỀU SITE WORDPRESS RIÊNG BIỆT (không phải Multisite)

Nếu mỗi subdomain là một WordPress riêng (không chia sẻ database), thì không dùng wildcard được dễ dàng, vì tất cả sẽ trỏ về cùng một folder. Bạn phải tạo separate VirtualHost cho từng site, ví dụ:

text

Và thêm từng domain vào file hosts.

Với cấu hình hiện tại + thêm dòng vào file hosts, bạn truy cập http://wp1.comarrow-up-right, http://sub1.wp1.comarrow-up-right, http://sub2.wp1.comarrow-up-right đều sẽ vào cùng folder – phù hợp cho Multisite.

Nếu vẫn lỗi, kiểm tra Apache error log: C:/xampp82/apache/logs/error.log để xem chi tiết. Chúc thành công!

Last updated