1. Core objectives and high-level functions
1.1 Core objectives
Change the URL structure of your WordPress site from the traditional Catalog formThe elegant transformation into a modern Sub-Domain Forms, and make sure it's search engine friendly.
- URL structure before conversion:
https://your-domain.com/[前缀]/[变量]/
- URL structure after conversion:
https://[变量].your-domain.com/
1.2 Advanced features (new program core)
This guide will implement a flexible switchable URL policy system that supports multiple domains.
- Multi-Domain Support: Can be used for multiple primary domains at the same time (e.g.
aigcsoft.site
,tchepai.com
) Enable subdomain mode. - URL Policy Switch: You can freely switch between the following two modes with a simple switch:
- Primary Domain Model (SEO Best): Regardless of the domain name from which the user accesses, the final URL and in-page links will be forced to be unified to the "main domain name" you specify in order to centralize the weight.
- Current Domain Model (Flexible Display): The website keeps the domain name that the user is currently visiting and generates all links under that domain name, for scenarios where multiple brands are displayed independently.
2. Preparatory work and core concepts
Before you start, make sure you have:
- DNS administrative privileges for all your associated domain names.
- Backend administrative privileges (e.g., Pagoda Panel, cPanel, etc.) of your web server (host).
This program relies on three major modules working together:
- DNS layer: serve as an "address book" that will be used by yourAll domain namesof subdomain requests to point to your server.
- Web server layer (Nginx/Apache):: Acts as a "traffic director", receives all requests, and creates a "traffic flow" for themultiple domain nameConfigure SSL and cross-domain policies.
- WordPress Application Layer (PHP):: Acting as a "content engine", the code intelligently recognizes requests, rewrites links according to on/off policies, and performs redirects.
3. Detailed configuration steps
Step 1: Configure DNS flood resolution for all domains
This is the basis. You will need to create a customized version of the file you plan to use for theEach primary domain nameRepeat this operation.
- placement: The DNS management panel for each domain name registrar.
- manipulate: Add a line for each domain name A RecordsThe
- Host Record (Host / Name):
*
(asterisk) - Record Type:
A
- Record Value (Value / Points to):
您服务器的 IPv4 地址
- typical example:: For
aigcsoft.site
Add an article*
Records, then for thetchepai.com
Add an article*
Records.
- Host Record (Host / Name):
Step 2: Configure the web server (using the Pagoda Panel as an example)
2.1. Adding Multi-Domain Binding and Multi-Certificate Configuration
- Create/Setup Site:
- placement: Pagoda Panel -> Site Management.
- manipulate: Create a site with the main domain name
*.your-domain-1.com
Then go to "Domain Management" for that site. Then go to the site's "Domain Management" and add your other wildcard domain names (such as*.your-domain-2.com
) Add it in.
- Configuring multiple SSL certificates (key step):
- placement: "Settings" -> "SSL" tab of the site.
- Apply for a certificate for your first domain name:
- Select "Let's Encrypt" and choose "DNS Authentication" for the authentication method.
- tick
your-domain-1.com
cap (a poem)*.your-domain-1.com
If you are not able to do so, follow the prompts to complete the TXT record validation for DNS and apply for it.
- Requesting a certificate for a second domain name:
- In the SSL interface, click on the top left corner of theCertificate brand drop-down menuThen, reselect "Let's Encrypt".
- The interface will refresh, allowing you to initiate aNew applicationsThe
- this timeCheck only
your-domain-2.com
cap (a poem)*.your-domain-2.com
If you want to apply for a new domain name, follow the prompts again to complete the DNS verification and application for the new domain name. - Repeat this for all domains. Eventually, your site will have multiple certificates deployed at the same time, and the server will automatically match them to the visiting domains.
- Force HTTPS: After all certificates have been successfully deployed, be sure to enable the "Force HTTPS" feature.
2.2. Configure CORS cross-domain policy (to resolve icon/font loading issues)
This configuration is done once and takes effect for all domains.
- placement: The Nginx or Apache configuration file for your site.
- manipulate:
- Nginx: In
server{}
Added within the block:# --- 为字体文件添加 CORS 跨域许可 --- location ~* \.(eot|otf|ttf|woff|woff2)$ { add_header 'Access-Control-Allow-Origin' '*' always; }
- Apache: in the root directory
.htaccess
file is added:# --- 为字体文件添加 CORS 跨域许可 --- <FilesMatch "\.(eot|otf|ttf|woff|woff2)$"> Header set Access-Control-Allow-Origin "*" </FilesMatch>
- Nginx: In
- Save and reload the service.
Step 3: Modify WordPress core files
3.1 Modifications wp-config.php
Files (located in the root directory of the website)
This file is responsible for identifying all subdomain requests for authorized domains.
manipulate: In wp-config.php
Top of the document (<?php
tag) add the following block of code.
/**
* ===================================================================
* 子域名泛解析配置: 内部请求重写 (多域名支持版)
* ===================================================================
*/
if (isset($_SERVER['HTTP_HOST'])) {
// --- 请在这里修改为您自己的配置 ---
$SUBDOMAIN_CONFIG = [
// 在此列表中添加所有您希望启用此功能的根域名
'allowed_main_domains' => [
'aigcsoft.site', // 您的第一个域名
'tchepai.com', // 您的第二个域名
],
// 这是您的固定目录前缀
'url_prefix' => 'tool',
];
// --------------------------------
$current_host = $_SERVER['HTTP_HOST'];
$host_parts = explode('.', $current_host);
if (count($host_parts) >= 3 && $host_parts[0] !== 'www') {
$subdomain = $host_parts[0];
$main_domain_candidate = implode('.', array_slice($host_parts, 1));
if (in_array($main_domain_candidate, $SUBDOMAIN_CONFIG['allowed_main_domains'])) {
$_SERVER['REQUEST_URI'] = '/' . $SUBDOMAIN_CONFIG['url_prefix'] . '/' . $subdomain . '/';
}
}
}
3.2 Modifications functions.php
file (located in the wp-content/themes/your-active-theme/
)
This file is the URL Policy Control Center.
manipulate: In your theme's functions.php
At the end of the file, add the followingComplete code blockThe
/**
* ===================================================================
* 子域名泛解析功能包: URL处理 & SEO重定向 (带开关的多域名支持版)
* ===================================================================
*/
// --- 请在这里修改为您自己的配置 ---
$MY_APP_CONFIG = [
// [重要!] 定义您的“官方”主域名。
'canonical_domain' => 'aigcsoft.site',
// 所有授权的域名 (与 wp-config.php 中保持一致)
'allowed_main_domains' => [
'aigcsoft.site',
'tchepai.com',
],
// 您的固定目录前缀
'url_prefix' => 'tool',
/**
* [总开关] 是否强制使用主域名 (Canonical Domain)
* - 设置为 true: 【SEO 最佳实践】无论用户从哪个域名访问,最终地址和页面内链接都会统一到 'canonical_domain'。
* - 设置为 false: 【灵活展示模式】网址会显示用户当前访问的域名,实现多品牌独立展示。
*/
'force_canonical_domain' => true, // 在这里切换 true 或 false
];
// --------------------------------
// 功能 1: 禁用 WordPress 规范链接重定向 (基石)
remove_filter('template_redirect', 'redirect_canonical');
// 功能 2: 动态重写网站内所有相关链接 (已升级)
function my_theme_rewrite_internal_links($permalink, $post) {
global $MY_APP_CONFIG;
$prefix_to_check = '/' . $MY_APP_CONFIG['url_prefix'] . '/';
if (strpos($permalink, $prefix_to_check) !== false) {
$target_domain = $MY_APP_CONFIG['canonical_domain'];
if (!$MY_APP_CONFIG['force_canonical_domain']) {
$current_main_domain = implode('.', array_slice(explode('.', $_SERVER['HTTP_HOST']), -2));
if (in_array($current_main_domain, $MY_APP_CONFIG['allowed_main_domains'])) {
$target_domain = $current_main_domain;
}
}
return 'https://' . $post->post_name . '.' . $target_domain . '/';
}
return $permalink;
}
add_filter('post_link', 'my_theme_rewrite_internal_links', 20, 2);
add_filter('post_type_link', 'my_theme_rewrite_internal_links', 20, 2);
// 功能 3: 为旧链接 (/prefix/slug/) 设置 301 永久重定向 (已升级)
function my_theme_redirect_old_urls() {
global $MY_APP_CONFIG;
$prefix_to_check = '/' . $MY_APP_CONFIG['url_prefix'] . '/';
if (in_array($_SERVER['HTTP_HOST'], $MY_APP_CONFIG['allowed_main_domains']) && strpos($_SERVER['REQUEST_URI'], $prefix_to_check) === 0) {
$slug = trim(str_replace($prefix_to_check, '', $_SERVER['REQUEST_URI']), '/');
if (!empty($slug)) {
$target_domain = $MY_APP_CONFIG['force_canonical_domain'] ? $MY_APP_CONFIG['canonical_domain'] : $_SERVER['HTTP_HOST'];
wp_redirect('https://' . $slug . '.' . $target_domain . '/', 301);
exit;
}
}
}
add_action('template_redirect', 'my_theme_redirect_old_urls');
// 功能 4: [新增] 当开关开启时,将非主域名访问重定向到主域名
function my_theme_redirect_to_canonical_domain() {
global $MY_APP_CONFIG;
if (!$MY_APP_CONFIG['force_canonical_domain']) return;
$current_host = $_SERVER['HTTP_HOST'];
$host_parts = explode('.', $current_host);
if (count($host_parts) >= 3 && $host_parts[0] !== 'www') {
$subdomain = $host_parts[0];
$current_main_domain = implode('.', array_slice($host_parts, 1));
if (in_array($current_main_domain, $MY_APP_CONFIG['allowed_main_domains']) && $current_main_domain !== $MY_APP_CONFIG['canonical_domain']) {
wp_redirect('https://' . $subdomain . '.' . $MY_APP_CONFIG['canonical_domain'] . '/', 301);
exit;
}
}
}
add_action('template_redirect', 'my_theme_redirect_to_canonical_domain', 5);
4. Final validation and troubleshooting
- Clearing the cache:: Upon completion of the modificationsnecessarilyClear all caches (plugins, CDNs, servers, browsers). It is highly recommended to useNo Trace ModeTesting.
- toggle switch: In
functions.php
modifications'force_canonical_domain'
The value of (true
/false
) to test different modes.
4.1. Verification method: When the switch is true
(host domain model)
- Test Jump: Type in the address bar of your browser
[变量].your-domain-2.com
. After pressing enter, the address bar shouldJump Nowuntil (a time)[变量].your-domain-1.com
(your primary domain name). - Test link: Visit any page and right-click to check all internal links, their
href
causalityMust be allPoint to your main domain (...your-domain-1.com
).
4.2. Verification method: When the switch is false
(current domain name mode)
- test hold:: Access
[变量].your-domain-2.com
The address bar shouldremain unchangedThe - Test link: In
[变量].your-domain-2.com
page, right-click to check all internal links, theirhref
attribute should point to the current domain (...your-domain-2.com
). - cross-testing:: New tab access
[变量].your-domain-1.com
The link to this page should then point to the...your-domain-1.com
The
4.3 Troubleshooting of common problems
- Unlimited redirection:: Ensure
remove_filter('template_redirect', 'redirect_canonical');
Exists and is not annotated. - Certificate error/insecurity: Check step 2.1 to ensure that independent SSL certificates have been successfully applied and deployed for all domain names.
- Icons/fonts missing: Check the CORS cross-domain configuration in step 2.2.
- Links or jumps do not behave as expected:: Inspection
functions.php
the value of the switch in the center is correct, andThorough Cache CleaningThe - Website white screen/500 error:: Inspection
wp-config.php
maybefunctions.php
There are no PHP syntax errors in the
Keep this advanced guide safe, it will be invaluable for your future server migrations, theme upgrades, or implementing the same strategy for new projects.