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

Laravel动态子域名应用下,主域名部署WordPress页面的实现方案

Laravel子域名应用与主域名静态营销站共存解决方案

方案1:Web服务器(Nginx/Apache)域名分流(推荐)

直接在服务器层面区分主域名与子域名请求,分别指向不同资源,完全不影响SEO,是最直接的解决方式。

Nginx配置示例

# 主域名:指向代理商营销站根目录
server {
    listen 80;
    listen [::]:80;
    server_name myapp.com www.myapp.com;
    root /var/www/marketing-static; # 替换为你的营销站实际目录
    index index.html index.htm;

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

# 子域名:指向Laravel应用public目录
server {
    listen 80;
    listen [::]:80;
    server_name *.myapp.com;
    root /var/www/laravel-app/public;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; # 根据你的PHP版本调整
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
  • 确保主域名(含www)和子域名泛解析(*.myapp.com)的A记录都指向服务器IP
  • 若用Apache,可通过VirtualHost配置实现相同逻辑,区分ServerName和ServerAlias

方案2:Laravel路由层面处理主域名请求

不想改动服务器配置时,可在Laravel路由中判断域名,直接返回静态营销站内容:

  1. 将代理商营销站的所有文件放到Laravel项目的public/marketing目录下
  2. 在routes/web.php中添加主域名路由组:
// 主域名路由:返回静态营销站内容
Route::domain('myapp.com')->group(function () {
    Route::get('/{any?}', function ($any = null) {
        $targetPath = public_path("marketing/{$any}");
        // 匹配存在的静态文件,否则返回首页
        if ($any && file_exists($targetPath) && !is_dir($targetPath)) {
            return response()->file($targetPath);
        }
        return response()->file(public_path('marketing/index.html'));
    })->where('any', '.*');
});

// 保留原有子域名路由逻辑
Route::domain('{subdomain}.myapp.com')->group(function () {
    // 你的子域名业务路由
});

此方案同样SEO友好,所有主域名请求直接返回静态资源,无需curl转发。

方案3:Laravel视图渲染营销站(适用于需少量动态交互的场景)

若营销站需要调用Laravel的部分功能(比如用户登录状态),可将营销站HTML转为Laravel视图:

  1. 将营销站文件复制到resources/views/marketing目录,调整资源路径为asset('marketing/css/xxx.css')这类格式
  2. 在routes/web.php中配置主域名路由:
Route::domain('myapp.com')->group(function () {
    Route::get('/', fn() => view('marketing.index'));
    Route::get('/about', fn() => view('marketing.about'));
    // 其他营销站页面路由
});

关于之前A记录未生效的排查方向

  • 检查主域名和子域名泛解析的A记录是否都正确指向服务器IP
  • 本地DNS缓存未刷新:执行ipconfig /flushdns(Windows)或sudo dscacheutil -flushcache(Mac)后重试
  • 服务器配置未区分域名:原配置中所有域名请求都指向Laravel应用,导致主域名仍显示Laravel内容

内容的提问来源于stack exchange,提问作者Eleonora Dirk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 06:01:22