1. Core objectives
Change the URL structure of your WordPress site from the traditionalCatalog formThe elegant transformation into a modernSub-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/
Key Placeholder Description:
your-domain.com
: Your primary domain name (e.g.:aigcsoft.site
).[前缀]
: A fixed directory name that you use to organize your content (for example:tool
,app
,showcase
).[变量]
: A unique identifier (slug) for an article or page (for example:tiktokalyzer-tool
).
2. Preparatory work and core concepts
Before you start, make sure you have:
- DNS administrative privileges for your web site's domain name.
- Back-end administrative privileges (e.g., Pagoda Panel, cPanel, Plesk, etc.) for your web server (host).
The realization of this solution relies on three major modules working in tandem:
- DNS layer: As an "address book", point all subdomains to your server.
- Web server layer (Nginx/Apache): Acts as the "traffic director", receiving all requests and handling cross-domain and other policies.
- WordPress Application Layer (PHP): Acts as a "content engine", intelligently recognizing requests, rewriting links and performing redirects.
3. Detailed configuration steps
Step 1: Configure DNS Flood Resolution
This is the basis for any subdomain to find your server.
- placement: Your domain name registrar's DNS management panel.
- manipulate: Add an article A RecordsThe
- Host Record (Host / Name)::
*
(asterisk) - Record Type::
A
- Record Value (Value / Points to)::
您服务器的 IPv4 地址
- TTL: Use the default value.
- Host Record (Host / Name)::
Step 2: Configure the Web Server
2.1. Adding a Flooded Site and Configuring SSL
- placement: Pagoda Panel or other hosting admin panel -> Website Management.
- manipulate::
- Add site: In the "Domain Name" field, enter
*.your-domain.com
, create the site. - Apply for SSL Certificate: Apply for a site for thewildcard certificateWhen using Let's Encrypt. When using Let's Encrypt, be sure to also verify that the
*.your-domain.com
cap (a poem)your-domain.com
The - Force HTTPS: After successfully deploying the certificate, enable the "Force HTTPS" feature.
- Add site: In the "Domain Name" field, enter
2.2. Configure CORS cross-domain policy (to resolve icon/font loading issues)
(coll.) fail (a student) [变量].your-domain.com
The page needs to load your-domain.com
This configuration is required for the font or icon files on the
- placement: YouMain site (
your-domain.com
) of the site profile. - manipulate::
- For Nginx users::
In the master's Nginx configuration file theserver{}
Inside the block, add the following code:# --- 为字体文件添加 CORS 跨域许可 --- location ~* \.(eot|otf|ttf|woff|woff2)$ { # 允许任何来源的页面请求此服务器上的字体文件。 add_header 'Access-Control-Allow-Origin' '*' always; }
- For Apache users::
In the root directory of the master.htaccess
file, add the following code:# --- 为字体文件添加 CORS 跨域许可 --- <FilesMatch "\.(eot|otf|ttf|woff|woff2)$"> Header set Access-Control-Allow-Origin "*" </FilesMatch>
- For Nginx users::
- Save the configuration file and reload the service.
Step 3: Modify WordPress core files
This is the core of the realization of all dynamic functions.
3.1 Modifications wp-config.php
Files (located in the root directory of the website)
This file is responsible for "translating" subdomain requests in the background.
manipulate: in wp-config.php
(used form a nominal expression) <?php
After the tag, add the following block of code.Be sure to change to your own domain name and prefix according to the comments!
/**
* ===================================================================
* 子域名泛解析配置: 内部请求重写
* ===================================================================
* 作用:当服务器接收到子域名请求时 (例如: a.b.com), 在 WordPress 内部
* 将其解析为对特定目录的访问 (例如: /prefix/a/),而浏览器地址
* 栏的 URL 保持子域名不变。
*/
if (isset($_SERVER['HTTP_HOST'])) {
// --- 请在这里修改为您自己的配置 ---
$main_domain_parts = ['your-domain-part-1', 'your-domain-part-2']; // 例如: ['aigcsoft', 'site']
$url_prefix = 'tool'; // 这是您的固定目录前缀, 例如: 'tool', 'app' 等。
// --------------------------------
$host_parts = explode('.', $_SERVER['HTTP_HOST']);
// 判断条件:域名由3部分组成, 且不是www, 且主域名匹配
if (count($host_parts) === 3 && $host_parts[0] !== 'www' && $host_parts[1] === $main_domain_parts[0] && $host_parts[2] === $main_domain_parts[1]) {
$subdomain = $host_parts[0];
// 将请求 URI 重写为/前缀/目录下的对应页面
$_SERVER['REQUEST_URI'] = '/' . $url_prefix . '/' . $subdomain . '/';
}
}
3.2 Modifications functions.php
file (located in the wp-content/themes/your-active-theme/
)
This file is responsible for three key tasks: preventing redirect loops, dynamically rewriting links, and handling 301 jumps for old links.
manipulate: In your current theme's functions.php
fileextremity, add the following complete block of code.Again, please change to your own domain name and prefix according to the comments!
/**
* ===================================================================
* 子域名泛解析功能包: URL处理 & SEO重定向
* ===================================================================
*/
// --- 请在这里修改为您自己的配置 ---
$MY_APP_CONFIG = [
'main_domain' => 'your-domain.com', // 您的主域名, 例如: 'aigcsoft.site'
'url_prefix' => 'tool', // 您在 wp-config.php 中设置的相同前缀, 例如: 'tool'
];
// --------------------------------
/**
* 功能 1: 禁用 WordPress 规范链接重定向
* 作用:这是整个方案的基石。防止 WordPress 将子域名请求自动301跳转回
* 带目录的 URL,从而避免无限重定向循环。
*/
remove_filter('template_redirect', 'redirect_canonical');
/**
* 功能 2: 动态重写网站内所有相关链接
* 作用:在页面生成时,自动将 /prefix/slug/ 格式的链接替换为 slug.domain.com
* 的子域名格式,实现完美的伪静态效果。
*/
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) {
$subdomain = $post->post_name; // 获取文章的 slug
return 'https://' . $subdomain . '.' . $MY_APP_CONFIG['main_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: 为旧链接设置 301 永久重定向
* 作用:当用户或搜索引擎直接访问旧的 /prefix/slug/ 格式的 URL 时,
* 自动执行一个 SEO 友好的 301 重定向,跳转到新的子域名地址。
*/
function my_theme_redirect_old_urls() {
global $MY_APP_CONFIG;
$domain_to_check = $MY_APP_CONFIG['main_domain'];
$prefix_to_check = '/' . $MY_APP_CONFIG['url_prefix'] . '/';
// 仅当访问的是主域名,且URL路径包含指定前缀时才执行
if ( $domain_to_check == $_SERVER['HTTP_HOST'] && strpos($_SERVER['REQUEST_URI'], $prefix_to_check) === 0 ) {
$slug = trim(str_replace($prefix_to_check, '', $_SERVER['REQUEST_URI']), '/');
if (!empty($slug)) {
$new_url = 'https://' . $slug . '.' . $domain_to_check . '/';
wp_redirect($new_url, 301); // 执行301永久重定向
exit;
}
}
}
add_action('template_redirect', 'my_theme_redirect_old_urls');
4. Final validation and troubleshooting
- Clearing the cache: Upon completion of all modifications.necessarilyClear WordPress plugin cache (e.g. WP Rocket), CDN cache (if any) and browser cache. It is highly recommended to useNo Trace ModePerform the test.
- Verify Link: Visit the home page of your website and right click to check the link to any of the tools/articles to see its
href
Has the attribute changed tohttps://[变量].your-domain.com/
Format. - Verify redirection: In the address bar of your browsermanual inputAn old address such as
https://your-domain.com/[前缀]/[变量]/
After pressing Enter, observe whether the address bar instantly jumps to the new subdomain address. - Validating subdomains: Visit a subdomain address directly to see if the page displays correctly and all icons and styles are loaded correctly.
FAQ Troubleshooting:
- Infinite Redirects (ERR_TOO_MANY_REDIRECTS): Inspection
functions.php
Have you added theremove_filter('template_redirect', 'redirect_canonical');
The - Icons/fonts are displayed as boxes or X's: Check that the CORS cross-domain configuration in step 2.2 has been added correctly and is in effect.
- Link unchanged: Inspection
functions.php
center$MY_APP_CONFIG
The domain names and prefixes in the site are exactly the same as those in your site. - Website white screen or 500 error: Inspection
wp-config.php
maybefunctions.php
Are there any PHP syntax errors (usually copy and paste errors).
Please keep this document in a safe place, as it will be invaluable to you in the future when you perform server migrations, theme upgrades, or implement the same strategy for new projects.