Angular 4 App on Apache

Asked

Viewed 866 times

0

I would like an example of htaccess to use in my Apache.

The scenario is as follows:

I have an Angular 4 application that needs to run on a separate back-end server. I build using the ng build command (for some internal problems I don’t use ng build --Prod) and place the generated files in the htdocs/app folder.

Because of the project’s need, I need to use relative urls so apache can redirect.

Ex: The url that would be http://www.app.com/app/something, must be api/app/Something.

I’ve searched Google and Stackoverflow and none of the suggested solutions for redirecting worked. My htacces are like this:

RewriteEngine on

RewriteRule "api/app" "http://localhost:8080/app/rest/$1" [P]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^(.*) index.html [NC,L]

The idea is that when I make a request in my Angular Service, it sees the api/app path and redirects to the localhost. I already have this proxy working in dev via angular-cli, but I need to use this also in apache.

Another thing is when I use my routes without the "#", it can’t reload. If I quit root ('/') it can follow the route, but when I update it gives error 404.

I hope I made myself clear. Thanks!

P.S. I don’t know Apache, so I’m getting it this way.

;D

2 answers

0

Francis:

I recently also had this difficulty.

When building, you cannot perform using the ng build --env=Prod command?

And check the generated html file, the href base of the document by default is "/" root. It is necessary to specify the proper path of your folder on the apache server.

0

ng build --Prod --base-href /app/

If you use gitbash so it is: ng build --Prod --base-href //app/

I hope it’s your solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.