服务器上的角度刷新返回404错误-使用.htacess

时间:2019-07-03 11:42:49

标签: angular apache .htaccess

我已经使用命令ng build --base-href ./ --prod构建了我的角度应用程序(我也尝试过ng build --base-href /index --prodng build --base-href ./index --prodng build --prod)。然后,我将在我的apache服务器上创建的dist文件夹的内容放入。

当我使用导航栏从主页转到另一页时,它工作正常。使用应用程序中的router进行设置。但是我按刷新,然后得到Error 404。我已经研究了这个问题并阅读了很多尝试的文章,我读了很长的git线程来尝试许多不同的组合。

我在网页的.htaccess根目录下创建了一个index.html

我已经多次看到这种情况,所以我的.htaccess看起来像这样:

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

为什么我仍然会收到404错误?我是在错误地构建应用程序还是在.htaccess中丢失了某些内容?

如果您需要从我的应用程序中获取更多代码,请告诉我,我将添加它,我不想添加可能无济于事的代码。

编辑 这是我的app-routing.module.ts

import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { SettingsComponent } from './settings/settings.component';


const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'settings', component: SettingsComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

1 个答案:

答案 0 :(得分:0)

我发现了exception filter文件的另一个版本,看起来像这样:

.htaccess

一旦建立,我就在RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/?(api)/ RewriteRule .* index.html [PT,L] 中设置<base href="./">。或使用index.html

进行构建
相关问题