Angular 8/Apache:htaccess用于root和sudomain应用程序



我有两个Angular应用程序,每个应用程序在Apache Web服务器(主机(的域的根目录中运行良好。

现在我喜欢把它们放入

App1 => https://example.com
App2 => https://app2.example.com

"app2"子域文件夹是根目录中的"/app2"。

如果我把App2放进子域目录,两个目录仍然运行,但App2只有在我不刷新页面的情况下才运行(我得到一个404找不到(。

我做了一些搜索,但大多数例子都集中在子目录而不是子域上,我尝试使用

ng build --prod --base-href "/app2/"

app2的build选项,但有很多可能的组合可以尝试,到目前为止我还没有得到任何一个。

因此,如果有任何帮助,我们将不胜感激,谢谢。

编辑:

也许解决方案很简单,但它让我抓狂。。。

如果我用标准.htaccess(Angular.io中的示例(:将Angular 8应用程序放入根目录,它们都可以正常工作

RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

我用我的提供者的工具创建子域。我能为子域配置的唯一选项是子域指向的"名称"one_answers"目录"。

在我的情况下(实时测试环境(:

http://example.com=>工作,文件是在根"/"与上面.htaccess.

http://admin.example.com=>不起作用,文件位于具有上述.htaccess.的子目录"/admin4711"中

当调用子域时,我会得到一个白色页面,在Chrome调试器中我可以看到以下错误:

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. polyfills-es2015.d08b2846b0483deb576c.js:1 
Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. runtime-es2015.de1f101ad38d40b1418d.js:1 
Uncaught SyntaxError: Unexpected token '<' scripts.36dbbe192861c60ce1c6.js:1 
Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. main-es2015.10848fe141a43f167ec1.js:1 

当在根目录中运行应用程序时,这些错误不会发生。

看起来好像web服务器对子域中的文件解释不正确。

已解决:

angular.io推荐的htaccess对于一个子目录是误导性的(至少对我来说是这样(。

答案是更换

%{DOCUMENT_ROOT}%{REQUEST_URI}

带有

%{REQUEST_FILENAME}

我会检查您的服务器/云设置。这听起来可能是DNS问题。

此外,请确保在设置好所有内容后清除本地浏览器缓存。否则,本地缓存可能会指向旧的设置信息,使您的网站无法加载。在过去,当我知道我的服务器设置正确,但我的网站仍然无法加载时,这让我很痛苦。

最新更新