要将WordPress的泛域名绑定到二级目录,你需要在你的服务器上修改Apache或Nginx配置文件。以下是两种最常见的服务器配置的示例:

Apache服务器

编辑你的虚拟主机配置文件,通常位于/etc/apache2/sites-available/目录下。

<VirtualHost *:80> ServerAdmin webmaster@yourdomain.com DocumentRoot /var/www/yourdomain.com/public_html ServerName yourdomain.com ServerAlias *.yourdomain.com

<Directory /var/www/yourdomain.com/public_html>
    Options -Indexes +FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    ReWriteEngine On

    # 将所有请求重定向到二级目录
    ReWriteCond %{HTTP_HOST} ^([^.]+)\.yourdomain\.com$
    ReWriteRule ^(.*) /subdirectory/%1/$1 [L]
</Directory>

</VirtualHost> 重新加载Apache配置:

sudo service apache2 reload Nginx服务器

编辑你的站点配置文件,通常位于/etc/nginx/sites-available/目录下。

server { listen 80; server_name yourdomain.com; root /var/www/yourdomain.com/public_html; index index.php index.html index.htm;

location / {
    try_files $uri $uri/ =404;
}

# 处理PHP请求
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

# 将所有请求重定向到二级目录
server {
    listen 80;
    server_name "*.yourdomain.com";

    location / {
        rewrite ^/(.*)$ /subdirectory/$subdomain.$domain/$1 last;
    }
}

} 重新加载Nginx配置:

sudo service nginx reload 请确保替换wodepress.com和/var/www/wodepress.com/public_html为你的实际域名和网站根目录,以及/subdirectory/为你的二级目录。

注意:在进行这些更改之前,请确保备份你的配置文件,并在完成后检查配置是否正确,以防止服务中断。同时,确保你的WordPress网站配置文件(wp-config.php)中已经正确设置了WP_HOME和WP_SITEURL常量,以反映新的域名结构。

原文

https://www.jianzhanpress.com/?p=7212