You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress中为基础URL添加自定义名称前缀

给域名添加全局URL前缀的实现方法

根据不同的站点类型,实现方式有所区别,以下是几种常见场景的操作步骤:

静态站点(纯HTML/CSS/JS)

  • 把所有静态文件统一放到一个名为test的子目录里,项目结构示例:
    my-project/
    ├── test/
    │   ├── index.html
    │   ├── about.html
    │   └── assets/
    │       ├── style.css
    │       └── script.js
    
  • 修改页面内所有链接和资源引用,把相对路径改成以/test/开头,比如:
    • 页面链接:<a href="/test/about.html">关于我们</a>
    • 样式引用:<link rel="stylesheet" href="/test/assets/style.css">

WordPress站点

  • 登录后台,依次点击「设置」→「常规」
  • 把「WordPress地址(URL)」和「站点地址(URL)」都改成https://mydomain.com/test
  • 保存后,登录地址会变成https://mydomain.com/test/wp-admin,所有页面、文章的URL都会自动带上/test/前缀
  • 补充:如果不想移动WordPress文件到test子目录,可修改wp-config.php,在/* That's all, stop editing! Happy publishing. */之前添加代码:
    define('WP_HOME','https://mydomain.com/test');
    define('WP_SITEURL','https://mydomain.com/test');
    

Nginx服务器配置

  • 打开Nginx站点配置文件(一般在/etc/nginx/sites-available/目录)
  • 在server块中添加location /test/规则,将请求转发到实际站点根目录:
    server {
        listen 80;
        server_name mydomain.com;
    
        location /test/ {
            alias /var/www/your-site/; # 替换成你的站点实际根目录路径
            index index.html index.php;
            # 若为PHP站点,添加PHP处理规则
            location ~ \.php$ {
                fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 根据你的PHP版本调整
                fastcgi_param SCRIPT_FILENAME $request_filename;
                include fastcgi_params;
            }
        }
    }
    
  • 保存后执行sudo nginx -t检查配置合法性,再执行sudo systemctl reload nginx使配置生效

Apache服务器配置

  • 方式一:修改Apache站点配置文件(通常在/etc/apache2/sites-available/)
    添加Alias规则:
    Alias /test /var/www/your-site
    <Directory /var/www/your-site>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    
  • 方式二:在站点根目录创建.htaccess文件(需开启mod_rewrite模块)
    添加重写规则:
    RewriteEngine On
    RewriteBase /test/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /test/index.php [L]
    
  • 保存后执行sudo systemctl reload apache2生效

内容的提问来源于stack exchange,提问作者Tiago Mano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 17:35:25