Angular - 在此服务器上找不到请求的 URL /home
Angular - The requested URL /home was not found on this server
我会很简短。
我有一个带有简单导航菜单 (routerLinks) 的 Angular 项目。
如果它在本地主机上,一切正常,由 angular cli.
提供支持
但是当我将它部署在真实服务器上时(我没有任何 VPS 或任何其他服务器,只有我可以放置文件的文件夹),奇怪的事情开始发生。
应用程序功能正常,导航功能正常,routeLinks 路由,但是当我刷新浏览器或尝试将某些内容手动写入 URL 行时,每次我都收到 404 未找到。 (所以我在 [domain]/home - 一切正常,但是当我刷新浏览器时,我得到 404 /home not found。
可能是我找的地方不好,不是angular的问题,而是HTTP请求的问题(我不是很懂)
你知道我应该从哪里开始吗?
谢谢你,Pavel F.
我正在谈论的项目:http://www.pavel-challenge.cz(不,这不是广告 :D)
app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { PassionsComponent } from './passions/passions.component';
import { QuestionComponent } from './question/question.component';
import { AboutComponent } from './about/about.component';
import { HomeComponent } from './home/home.component';
import { KnowledgeComponent } from './knowledge/knowledge.component';
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'knowledge', component: KnowledgeComponent },
{ path: 'questions', component: QuestionComponent },
{ path: 'passions', component: PassionsComponent },
{ path: '**', redirectTo: '/home', pathMatch: 'full' }
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
navbar.component.html
<ul>
<li><a routerLink="/about">About</a></li>
<li><a routerLink="/knowledge">Knowledge</a></li>
<li><a routerLink="/questions">Questions</a></li>
<li><a routerLink="/passions">Passions</a></li>
</ul>
问题出在您的 HTTP 服务器配置上。
您必须使主机名后的所有 URL 指向嵌入您的 angular 应用程序的 HTML 资源。
就像在 NGINX 中一样:
location / {
root <path to index.html>;
try_files $uri /index.html =404;
( if ressource exist serves it, likes images, else load index.html to load angular app )
}
here is the official documentation
在初始加载后导航时,angular 路由器带头,并在没有 http 请求的情况下动态显示位置更改后的数据。
但是如果您直接加载到 <...>/home,http 服务器肯定会尝试加载文件 home.html,然后发送 404。
已修复:(对于 Apache HTTP 服务器)
我创建了 .htaccess 文件并将其放入提供商端的根文件夹中。
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
如果您在 Apache 上部署,您应该启用 mod_rewrite 模块:
sudo a2enmod 重写
然后重启Apache,sudo systemctl restart apache2
然后根据之前的回答在 .htaccess 中进行建议的更改
我会很简短。
我有一个带有简单导航菜单 (routerLinks) 的 Angular 项目。 如果它在本地主机上,一切正常,由 angular cli.
提供支持但是当我将它部署在真实服务器上时(我没有任何 VPS 或任何其他服务器,只有我可以放置文件的文件夹),奇怪的事情开始发生。
应用程序功能正常,导航功能正常,routeLinks 路由,但是当我刷新浏览器或尝试将某些内容手动写入 URL 行时,每次我都收到 404 未找到。 (所以我在 [domain]/home - 一切正常,但是当我刷新浏览器时,我得到 404 /home not found。
可能是我找的地方不好,不是angular的问题,而是HTTP请求的问题(我不是很懂)
你知道我应该从哪里开始吗?
谢谢你,Pavel F.
我正在谈论的项目:http://www.pavel-challenge.cz(不,这不是广告 :D)
app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { PassionsComponent } from './passions/passions.component';
import { QuestionComponent } from './question/question.component';
import { AboutComponent } from './about/about.component';
import { HomeComponent } from './home/home.component';
import { KnowledgeComponent } from './knowledge/knowledge.component';
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'knowledge', component: KnowledgeComponent },
{ path: 'questions', component: QuestionComponent },
{ path: 'passions', component: PassionsComponent },
{ path: '**', redirectTo: '/home', pathMatch: 'full' }
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
navbar.component.html
<ul>
<li><a routerLink="/about">About</a></li>
<li><a routerLink="/knowledge">Knowledge</a></li>
<li><a routerLink="/questions">Questions</a></li>
<li><a routerLink="/passions">Passions</a></li>
</ul>
问题出在您的 HTTP 服务器配置上。
您必须使主机名后的所有 URL 指向嵌入您的 angular 应用程序的 HTML 资源。
就像在 NGINX 中一样:
location / {
root <path to index.html>;
try_files $uri /index.html =404;
( if ressource exist serves it, likes images, else load index.html to load angular app )
}
here is the official documentation
在初始加载后导航时,angular 路由器带头,并在没有 http 请求的情况下动态显示位置更改后的数据。
但是如果您直接加载到 <...>/home,http 服务器肯定会尝试加载文件 home.html,然后发送 404。
已修复:(对于 Apache HTTP 服务器)
我创建了 .htaccess 文件并将其放入提供商端的根文件夹中。
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
如果您在 Apache 上部署,您应该启用 mod_rewrite 模块:
sudo a2enmod 重写
然后重启Apache,sudo systemctl restart apache2
然后根据之前的回答在 .htaccess 中进行建议的更改