Dcortez
Dcortez

Reputation: 4238

Tomcat RewriteCond

I would like to write RewriteCond in tomcat rewrite.config so all urls (except api ones) that and with no extension or html one are redirected to /index.html. I would like to do it so refresh in angular app starts to work.

I already have something like

RewriteCond %{REQUEST_URI} ^[^.]*(html?)?$
RewriteRule ^(.*)$ /index.html [L]

But that matches all urls which means that now api calls are not working. So I would like to exclude urls containing /api element. Is it possible to do it this way or should I try to specify cond other way round, that means using negation operator and instead trying to specify resources that shoud not be matched? I have already tried few ways to achieve this but none is working.

Upvotes: 2

Views: 840

Answers (2)

Fabian
Fabian

Reputation: 175

This is a pretty old post but maybe this will still help someone. I was searching a long time and tried a lot of different suggestions.

My angular frontend is running under resources/public in a java spring boot project. The WAR is running in a tomcat.

The examples of rewrite.config on google looked like the once of @Dcortez and I got the same results as he describes. After trying a lot if found out the regex is the problem.

My actual rewrite.config:

RewriteCond %{REQUEST_PATH} !-f  
RewriteRule ^.*\/project-folder\/.[a-zA-Z0-9]* /example.project/angular-frontend/index.html

And I build the angular app like this:

ng build --base-href /example.project/angular-frontend/

Upvotes: 2

Dcortez
Dcortez

Reputation: 4238

Not an exact answer I was looking for, but finally i managed to achieve my goal simply by changing angular routing strategy to hashing one. Then refreshing suddenly started to work. So after all I didn't write any rule for Tomcat rewriting.

Upvotes: 0

Related Questions