Angular2 routing / deep linking not working with Apache 404

空扰寡人 提交于 2019-11-27 00:33:13

As the above answer doesn't really answer the question for if you want it to work with Apache. HashLocationStrategy remains an option, but if you want it to work as is on Apache you need to do the following:

Create a new file .htaccess in the same location as your index.html. The following code will be inside:

<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>

(Note that in the question the <base href="/crises-center/"> inside the index.html should be identical to the RewriteBase)

Answer adapted from question+answer from Angular 2 routing and direct access to a specific route : how to configure Apache?

For those who's running on Apache use this .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>

If you still get an error run those 2 commands:

sudo a2enmod rewrite
sudo systemctl restart apache2

For angular4/angular in app.routes.module.ts include useHash as below,

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

You have to make your server handle all routes back to index.html or use HashLocationStrategy

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

Take a look at:

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

According to Rein Baarsma answer you have to create .htaccess file and put it in the same directory as your index.html on your Apache server. It might be for example htdocs directory, but this depends on your Apache configuration.

However I use different .htaccess file content and it works for me for all links/routes without necessity to specify rewrite base:

<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>

It is a modification of code provided by Rein Baarsma.

My app is Angular 2+ (4.x.x) application based on angular-cli project. It hardly depends on direct URLs to addresses provided by Angular routing. Without .htaccess file above I got 404 not found after clicking every direct URL link. Now with use of this .htaccess file everything works fine.

dheeraj kumar

From refreshing the page results in 404 error- Angular 6:

1) Put below code in web.xml of your deployment folder :

<error-page>
     <error-code>404</error-code>
     <location>/index.html</location>
</error-page>

If you are working with the ionic build and upload your site on hosting server then you need to add the .htaccess file on your root directory inside the www folder.

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!