I have successfully installed and ran Angular 2 app on my apache server, I am able to navigate through the pages via [routerLink]="['/register']"
However when I refresh the page I get a 404 error on the register page. Is there something wrong with my rewrite rules:
Options +FollowSymLinks
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</ifModule>
Also here is my apache VirtualHost
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/front/dist/browser
<Directory /var/www/html/front>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
4
Answers
Yes, As per I understand, your rewrite rules don’t seem correct to me..
You are saying if the requested document is not a file AND not a Directory AND I guess not the index page then redirect to
index.html
and what if they are?? And why don’t you want to go to the index page? I believeRewriteCond %{REQUEST_URI} !index
means you don’t want to go to index.I don’t know what is your requirement but for a simplistic reqirement I use:
Try This
Check this:https://github.com/angular/angular-cli/issues/1630
Ref:https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/
you can solve it by redirecting 404 Error to your index.html
– In Apache Server hosting, edit Apache configuration:
sudo nano /etc/apache2/sites-enabled/000-default.conf
– Add ErrorDocument 404 like this:
my index.html is found in /var/www/html/vas/index.html
– Full file:
– Restart Apache server by cmd sudo service apache2 restart
Try creating a .htaccess file on the root of your application with the below rules,