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

Apache Alias配置:NextJS与Angular同域不同根目录刷新404问题

问题分析与解决方案

直接访问或刷新Angular页面出现404,核心原因是Angular作为单页应用(SPA),其路由是前端路由,Apache默认会把请求当作静态文件路径去查找,找不到就返回404。你当前的配置只完成了路径映射,缺少针对SPA的重写规则,需要补充以下配置:

1. 修正目录匹配并添加SPA重写规则

你的<Directory>标签路径和Alias不匹配,同时需要添加规则将非文件/非目录的请求转发到Angular的index.html:

Alias /en/framesite/Bellas/103 /var/www/html/app-live/dist/en/framesite/Bellas/103
<Directory "/var/www/html/app-live/dist/en/framesite/Bellas/103/">
    RewriteEngine On
    Require all granted
    AllowOverride All
    # 仅当请求不是真实文件/目录时,转发到index.html
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /en/framesite/Bellas/103/index.html [L]
</Directory>

2. 给根Alias /en添加SPA规则(若/en下也是Angular应用)

如果/en路径下同样部署了Angular应用,需要给对应目录补充相同逻辑的重写规则:

Alias /en /var/www/html/app-live/dist/en
<Directory "/var/www/html/app-live/dist/en/">
    RewriteEngine On
    Require all granted
    AllowOverride All
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /en/index.html [L]
</Directory>

3. 重启Apache生效

修改配置后执行命令重启服务:

service httpd restart

额外注意

确保Angular打包时的base-href配置正确,比如部署在/en/framesite/Bellas/103路径下,打包需执行:

ng build --base-href /en/framesite/Bellas/103/

否则Angular前端路由可能出现路径错误。

内容的提问来源于stack exchange,提问作者CHIP-1106

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 00:07:16