Dcortez
Dcortez

Reputation: 4228

Spring\Tomcat redirect to angular page on 404 error

I have an application written in Spring (rest api) and deployed on Tomcat. On the same context exists angular 4 application using this api. I would like to redirect all 404 errors to error page in angular app. I managed to redirect all url in pattern \app\#\some\url by using appropriate angular route. But this those not help with url like \app\some\url which are not handled by angular. In case of such url Tomcat returns it's default page. I managed to redirect all 404 error to angular main page by using following code in web.xml file.

<error-page>
    <error-code>404</error-code>
    <location>/</location>
</error-page>

But when I changed / for /#/404 (error page form angular app) and tried to access non-existent url I just get blank page instead of redirection. Is there a way to configure my app or server to redirect to this error page? Or do I have to copy this page and put it in WEB-INF so I can use it as static page? I would like to avoid second option as in this case I have to duplicate this page and in case of changes remember about both of them.

Upvotes: 1

Views: 1211

Answers (2)

Shubhendu Vaid
Shubhendu Vaid

Reputation: 567

1) Using hash # in URLs will solve your problem. 2) Another solution would be to have a rewrite rule on your Apache or ngnix servers so that they redirect to index everytime.

Upvotes: 2

herokingsley
herokingsley

Reputation: 403

it is because in web.xml,the location it's to track down the file. So the value you put in here must be a file path.

My perspective, you can use a jsp to redirect the user into the actual uri path /#/404.

Upvotes: 0

Related Questions