Angular-cli: How to run angular 2 application on apache hosting server

Created on 24 Sep 2016  路  6Comments  路  Source: angular/angular-cli

Hi Guys,

I have hosting plan and i want to host my angular 2 application on the hosting server which have apache server.

The hosting server don't have a npm install.

after below command and getting a like the strc

file strc

image
image

image

ng build --prod

and uploading to my hosting server, when i am accessing the my url

http://www.exmaple.com/ -- it's working properly
http://www.exmaple.com/home -- it's giving a error, the file is no found on the server - 404

but the same thing working on my machine, when i am trying using a cmd
ng serve - command

Most helpful comment

Since you are building a SPA, I think the problem is with your apache configuration. You need to enable the rewrite module and add config to .htaccess to redirect all router to indeed.html.

Something like this:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^ index.html [L]

</IfModule>

Your server is trying to send you the response but it will not find anything there, angular needs to handle the routes.

All 6 comments

Unless you're using the HashLocation strategy you're going to run into 404s. Have a look at this https://github.com/mgechev/angular2-seed/wiki/Deploying-prod-build-to-Apache-2

Since you are building a SPA, I think the problem is with your apache configuration. You need to enable the rewrite module and add config to .htaccess to redirect all router to indeed.html.

Something like this:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^ index.html [L]

</IfModule>

Your server is trying to send you the response but it will not find anything there, angular needs to handle the routes.

Closing as a couple of good responses are here already.

I was still having this issue, even with the rewrites. I wanted to mention my solution for anyone still having the issue:

I was including MultiViews in Options Indexes FollowSymLinks MultiViews in my Apache configuration. Removing it will fix the problem. According to the documentation:

The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo., and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.

This was causing a 404 for any route other than /.

try to using this rewrite rule for 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]

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings