László Csiki
László Csiki

Reputation: 135

.htaccess redirect in case of 404 error

I have an angular app with several routes, for example:

/collections
/collections/1/1
/servers
/servers/1

And my app is placed in /var/www/html/myapp/ The myapp folder contains the whole project and the index.html file. In the index.html file the base is set to <base href="/myapp/">.

And i have a .htaccess file in this folder which contains the rewrite rules

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

When i am trying to enter to http://dummydomain.com/myapp/ it's working fine, but when i enter to http://dummydomain.com/myapp/collections it's not working.

So my question is how can i redirect to index.html when 404 error is directly requested? Thank you!

Upvotes: 1

Views: 2551

Answers (1)

anubhava
anubhava

Reputation: 784898

Your RewriteBase directive and / in front of index.html will forward requests to site root instead of current directory.

You can use:

DirectoryIndex index.html

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteRule ^index\.html$ - [L,NC]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.html [L]
</IfModule>

Upvotes: 3

Related Questions