1

I have created an Angular application and it is working fine. If I try to access a specific page on the browser I can type the route and it will redirect me there. Now the backend shares this route in mail and I would like to redirect to this page.

Accessing the page however, throws 404 error.

I am using cPanel as my hosting service.

Any help would really appreciate. Been trying it for days now.

I have tried changing .htaccess to have RewriteEngine On but still not working as expected.

Khaled Ayed
  • 1,121
  • 3
  • 12
  • 29
  • I hope this help answer your concern: https://stackoverflow.com/questions/22739455/htaccess-redirect-for-angular-routes – Khaled Ayed Jul 20 '23 at 10:56
  • 1
    Thanks @KhaledAyed I was able to combine this and removed the / in my router which was [routerLink]="['/home']" to [routerLink]="['home']". Need to find out whether this was the cause though. – Nelson Omoi Jul 21 '23 at 12:49

2 Answers2

0

Check your Nginx configuration file. It should be similar to:

server{
    listen 80;
    listen [::] 80;
    server_name www.example.com example.com;
    root /var/www/example.com;
    index index.html;
    location / {
        try_files $uri$args $uri$args/ /index.html;
    }
}

Updated:

I have never tried that, but something similar to nginx config above but for .htaccess is:

    <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>
Evgeny Gurevich
  • 475
  • 3
  • 5
  • I would have also prefered this but seems the Cpanel am using only give me access to do file transfers and the /etc/nginx folder is not available. I only have access to .htaccess file. – Nelson Omoi Jul 19 '23 at 19:33
0

Add a rewrite rule to the .htaccess as below.

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