生产中的角度布线-htaccess

时间:2019-04-28 11:31:59

标签: angular apache .htaccess

我有一个angular 7应用程序,我在其中使用这样的路由:

const routes: Routes = [
  {path: '', component: HomePageComponent},
  {path: 'privacy', component: PrivacyPageComponent},
  {
    path: '',
    redirectTo: '',
    pathMatch: 'full'
  },
  {path: '**', component: PageNotFoundComponent}
];'

在localhost中,一切正常。如果我在浏览器的地址栏中输入:

  • localhost:4000 /->我转到主页
  • localhost:4000 / privacy->我转到隐私页面

问题是当我将其部署到生产环境(Apache)中时。我已经按照Angular文档的建议编辑了.htaccess。

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

但是在这种情况下(当然),如果我在浏览器中输入:

  • localhost:4000 /->我转到主页
  • localhost:4000 / privacy->我再次进入主页

只有当我转到主页并单击带有routerLink =“ / privacy”的链接时,路由基本上才有效。如果用户直接在浏览器的地址栏中输入完整的URL,则无法访问该页面。

我该怎么做才能使Angular服务定义的所有页面?

谢谢!

1 个答案:

答案 0 :(得分:0)

我终于找到了这篇文章:http://krasimirtsonev.com/blog/article/apache-htaccess-for-html5-push-state-manipulations

这篇文章的标题并不能完全解决我的问题,但是无论如何,我还是在这里张贴文章供可能会遇到相同问题的其他人使用:

基本上,我解决了编辑.htaccess的问题,这就是整个过程。

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html