【问题标题】:Angular2 routing / deep linking not working with Apache 404Angular2 路由/深度链接不适用于 Apache 404
【发布时间】:2016-04-21 08:08:55
【问题描述】:

我正在关注 Angular 2 路由示例。使用“lite”网络服务器,我可以从根目录导航,并且深度链接可以工作,但是使用 Apache,我可以从根目录导航,但是在直接链接到路由时会出现 404 Not Found 错误。

例如,以下 URL 适用于由 npm 在端口 3000 上启动的“lite”网络服务器。

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

但是下一个针对 Apache 在端口 80 上运行的 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]);

【问题讨论】:

  • 它可以与angular.io/docs/js/latest/api/router/… 一起使用吗?
  • 我确实设法让 PathLocationStrategy 在 Apache 上运行以下 .htaccess 我之前的尝试失败了,因为 AllowOverride 和 mod_rewrite 模块配置存在问题。 RewriteEngine On # 如果请求现有资产或目录,则按原样转到它 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d RewriteRule ^ - [L] # If请求的资源不存在,使用 index.html RewriteRule ^ /index.html

标签: angular angular2-routing


【解决方案1】:

你必须让你的服务器处理所有返回 index.html 的路由或使用HashLocationStrategy

(https://angular.io/docs/ts/latest/api/common/index/HashLocationStrategy-class.html)

看看:

Is Angular 2's Router broken when using HTML5 routes?

【讨论】:

  • 谢谢,stackoverflow 链接很有用,github 上有一个关于强制前端开发人员修改 webserver 配置的相当有趣的问题。我预测当人们在轻量级网络服务器上开发然后尝试部署到现实世界时会浪费很多时间。
  • 是的,我个人不喜欢他们将此模式设置为默认模式而没有任何其他警告的事实,但如果您切换到 HashLocationStrategy 它应该可以工作,而无需您在后端服务器中执行任何操作.
  • 我已切换到 HashLocationStrategy,因为这似乎问题最少。
【解决方案2】:

由于其他答案并不能真正回答您是否希望它与 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 中的&lt;base href="/crises-center/"&gt; 应该与RewriteBase 相同)

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

【讨论】:

  • 谢谢。我放弃了让它与 Apache 一起工作,并最终让 PathLocationStrategy 与 nginx 一起工作。我相信大多数问题都在 Angular 方面,添加以下内容似乎可以解决这些问题。 {path: '/**', redirectTo: ['Welcome']},
  • 提供的 HashLocationStrategy 链接当前不起作用,并且链接名称中有错字。有HasLocationStrategy 应该是HashLocationStrategy :)。当前工作链接:HashLocationStrategy
  • RewriteRule . /index.html [L] 对我不起作用。但是RewriteRule . index.html [L] 可以(没有/)。
  • 它对我不起作用。我正在尝试直接访问特定路由。并配置了 Apache。我的基本 URL 和 .htaccess 文件 选项索引 FollowSymLinks RewriteEngine On RewriteBase /// RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule 。 index.html [L] 请帮帮我
  • 谢谢@MA-Maddin,为我解决了这个问题。似乎 /index.html 指向服务器根目录并且无法识别 RewriteBase。知道为什么吗?
【解决方案3】:

根据 Rein Baarsma 的回答,您必须创建 .htaccess 文件并将其放在与 Apache 服务器上的 index.html 相同的目录中。例如,它可能是 htdocs 目录,但这取决于您的 Apache 配置。

但是我使用不同的 .htaccess 文件内容,它适用于所有链接/路由,无需指定重写基础:

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

这是对 Rein Baarsma 提供的代码的修改。

我的应用程序是基于 angular-cli 项目的 Angular 2+ (4.x.x) 应用程序。它几乎不依赖于 Angular 路由提供的地址的直接 URL。如果没有上面的.htaccess 文件,我在单击每个直接 URL 链接后得到了404 not found。现在使用这个.htaccess 文件一切正常。

【讨论】:

    【解决方案4】:

    对于 app.routes.module.ts 中的 angular4/angular 包括 useHash 如下,

    @NgModule({
      imports: [RouterModule.forRoot(routes, {useHash: true})],
      exports: [RouterModule]
    })
    

    【讨论】:

    • 感谢投反对票,我能知道原因吗?当人们搜索 angular4 路由、深度链接时,此页面会出现在搜索结果中。所以正如解释的那样,我也为 angular4 添加了一个解决方案。不要再无缘无故地做懦夫和投反对票了。我需要时间来添加这个解决方案,我只是想帮助和我一样的人。
    • 谢谢亚拉姆。它对我有用,但我有一个问题......我的 URL 以“#/”开头我该如何解决这个问题。我不想在 url 中看到 #
    • 因为你使用了哈希策略@FaridAbbas。 Angular 使用的默认策略是路径定位策略,它具有许多功能,如 SEO 友好等。请看这篇文章 codecraft.tv/courses/angular/routing/routing-strategies
    • 投了反对票,因为任何体面的开发人员都不应该在没有特殊原因的情况下在 angular2 应用程序中使用主题标签...
    • 使用散列是一种技巧,仅当您在同一个代理上构建全栈时才有效(当您在同一个域上提供 API 服务并且您需要分离时)
    【解决方案5】:

    对于那些在 Apache 上运行的人 使用这个 .htaccess

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
    
        RewriteRule ^(.*) /index.html [NC,L]
    </IfModule>
    

    如果您仍然遇到错误,请运行以下 2 个命令:

    sudo a2enmod rewrite
    sudo systemctl restart apache2
    

    【讨论】:

    • 这对 Angular 4+ 非常有效;正是我想要的,因为它阻止了 404 页面刷新。
    • 如何在上面的htaccess-file中添加HTTPS-redirect?
    【解决方案6】:

    如果您正在使用 ionic 构建并在托管服务器上上传您的网站,那么您需要在 www 文件夹内的根目录中添加 .htaccess 文件。

        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^(.*) / [R=302,NC,L]
    
    
    

    【讨论】:

      【解决方案7】:

      在 httpd.conf 或 apache2.conf 中添加以下内容:

      <Directory "/var/www/html/">
          Options FollowSymLinks
          Allow from all
          AllowOverride All
      </Directory>
      
      • 将“/var/www/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
      

      【讨论】:

        【解决方案8】:

        这个生成器适用于我和我的使用 Apapche HTTPD 的 HostMonster 帐户。它生成的 .htaccess 文件开箱即用:

        https://julianpoemp.github.io/ngx-htaccess-generator/#/generator

        # Generated with ngx-htaccess-generator v1.0.7
        # https://julianpoemp.github.io/ngx-htaccess-generator/
        
        
        <IfModule mod_rewrite.c>
          RewriteEngine On
          
          # Redirection of requests to index.html
          RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -s [OR]
          RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
          RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
          RewriteRule ^.*$ - [NC,L]
          # Redirect all non-file routes to index.html
          RewriteRule ^(?!.*\.).*$ index.html [NC,L]
        </IfModule>
        

        谢谢你,朱利安·庞普!

        【讨论】:

          猜你喜欢
          • 2016-10-10
          • 2017-03-05
          • 1970-01-01
          • 2017-01-04
          • 2017-04-12
          • 1970-01-01
          • 1970-01-01
          • 2018-10-02
          • 1970-01-01
          相关资源
          最近更新 更多