I have Apache server which is running Angular 6 application under /var/www/html/<angular_root>. I tried to add one folder /var/www/html/admin/<angular_root> but I get errors Failed to load resource: the server responded with a status of 404 (Not Found). Do you know what configuration I need to implement?
My current apache configuration is:
<Directory /var/www/html/admin>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
4 Answers 4
Your angular project(s base href is wrong. when you deploy your application, specify base-href on the cmdline :
ng build --prod --base-href ./
for reference https://angular.io/guide/deployment
1 Comment
--prod is deprecated. Use ng build --configuration productionHere is my working example:
<VirtualHost fancy.local:80>
DocumentRoot "/abs/path/to/fancy/dist/browser"
ServerName fancy.local
<Directory "/abs/path/to/fancy/dist/browser">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Where dist/browser is a generated sources with index.html
1 Comment
Options should not be defined with "Indexes", "FollowSymLinks", "Includes" and "ExecCGI" for security reasons.You need to add rewrite rules to .htaccess. Otherwise the requests to your routes won't be redirected back to index.html for rendering. Look at the following link for Apache deployment: https://angular.io/guide/deployment
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
Comments
One of my project which was on tomcat server, I have done the following:
- Create a new folder inside
src/WEB-INF. - Inside
WEB-INFfolder createweb.xmlwith below code.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<error-page>
<error-code>404</error-code>
<location>/</location>
</error-page>
</web-app>
- Update your
angular.jsonbuild> assets section, when you build your project it will get copied to final./distfolder.
"assets": [
{
"input": "src/assets",
"output": "/assets/"
},
"src/favicon.ico",
"src/WEB-INF"
],
- Update your
index.htmlfile<base href="./">
Hope this will helps someone.
DocumentRootis set to/var/www/html, and since you are usingadmindirectory, you should have used base ref as '/admin' instead of default '/' at the time of compilation. You can later change it within generatedindex.htmlif you wish. @DmitryK has usedDocumentRootsame as path of directory to host Angular code, so default base ref is working fine.