加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular2路由/深层链接不能与Apache 404一起使用

发布时间:2020-12-17 08:03:50 所属栏目:安全 来源:网络整理
导读:我正在关注Angular 2路由示例。使用“精简”网络服务器,我能够从根和深层链接工作导航,但使用Apache我可以从根导航,但是当跟随链接指向路由时,会获得404 Not Found错误。 例如,以下URL对npm由端口3000启动的“lite”Web服务器起作用。 http://localhost
我正在关注Angular 2路由示例。使用“精简”网络服务器,我能够从根和深层链接工作导航,但使用Apache我可以从根导航,但是当跟随链接指向路由时,会获得404 Not Found错误。

例如,以下URL对npm由端口3000启动的“lite”Web服务器起作用。

http://localhost:3000/crisis-center/2

但是在端口80上运行Apache的下一个URL失败了。

http://localhost/crisis-center/2
The requested URL /crisis-center/2 was not found on this server.

我确实尝试了一些针对类似Angular 1问题推荐的.htaccess解决方案,但没有运气。如果有人在Apache上有Angular 2路由和深层链接工作,请告诉我你是如何实现的。

@RouteConfig([
{ // Crisis Center child route
path: '/crisis-center/...',name: 'CrisisCenter',component: CrisisCenterComponent,useAsDefault: true
},{path: '/heroes',name: 'Heroes',component: HeroListComponent},{path: '/hero/:id',name: 'HeroDetail',component: HeroDetailComponent},{path: '/disaster',name: 'Asteroid',redirectTo: ['CrisisCenter','CrisisDetail',{id:3}]}
])

Boot.ts

import {bootstrap}        from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {AppComponent}     from './app.component';
bootstrap(AppComponent,[ROUTER_PROVIDERS]);
因为上面的答案并没有真正回答你是否想要它与Apache一起工作的问题。 HashLocationStrategy仍然是一个选项,但如果您希望它在Apache上工作,您需要执行以下操作:

在与index.html相同的位置创建一个新文件.htaccess。以下代码将在里面:

<IfModule mod_rewrite.c>
  Options Indexes FollowSymLinks
  RewriteEngine On
  RewriteBase /crisis-center/
  RewriteRule ^index.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

(注意,在问题中,index.html内的< base href =“/ crises-center /”>应该与RewriteBase相同)

答案改编自Angular 2 routing and direct access to a specific route : how to configure Apache?的问答

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读