我有一个运行在apache web服务器上的单页网站,使用angular cli编写。我有一个主页和多个链接,使用routerlink标签导航,工作正常。然而,当我点击"about",例如,它导航到website.net/about,一切都是完美的,直到页面重新加载,或者当它抛出404错误时,我手动输入website.net/about。
这是应用程序的主要代码:
index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule} from '@angular/router';
import { AppComponent } from './app.component';
import { ProgramsComponent } from './programs/programs.component';
import { AboutComponent } from './about/about.component';
@NgModule({
declarations: [
AppComponent,
ProgramsComponent,
AboutComponent
],
imports: [
BrowserModule,
RouterModule.forRoot([{
path: 'programs',
component: ProgramsComponent
},
{
path: 'about',
component: AboutComponent
}
])
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts:
import { Component } from '@angular/core';
import {ProgramsComponent} from './programs/programs.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Title';
}
app.component.html:
<div id="header" style="text-align:Left">
<header>
<a href="../index.html">
<h1>
{{ title }}
</h1>
</a>
<nav>
<div class="nav-comp">
<a routerLink="/about">About</a>
<a routerLink="/programs">Programs</a>
</div>
</nav>
</header>
</div>
<router-outlet></router-outlet>
任何帮助和/或链接将不胜感激,如果这可以使用apache web服务器修复,我会开放的,但我不希望这样做
发布于 2018-05-27 10:50:27
我猜问题出在您的apache配置中,显然您的代码没有任何问题。
发布于 2018-05-29 07:16:47
好的,所以我不得不做一些挖掘,我想在reddit上感谢u/vaskemaskine来获取.htaccess
文件,在this post上使用sci buff,但现在我要开始了
因此,首先在/var/www/html
中,我必须创建一个名为.htaccess
的文件,并将以下代码放入其中
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
然后为了实现这一点,由于Apache2.4中的一个变化,我不得不通过修改AllowOverrides
in /etc/apache2/apache2.conf
来实现
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
至
<Directory />
Options FollowSymLinks
AllowOverride All
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride All
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
但是,我在运行RewriteEngine
时遇到错误,因为它并未安装,但只需在终端中运行sudo a2enmod rewrite && sudo service apache2 restart
即可将其清除
https://stackoverflow.com/questions/50548397
复制相似问题