Tomcat的8 URL重写重写、Tomcat、URL

2023-09-13 05:00:40 作者:浅笑嫣然

我有一个AngularJS web应用和泽西后端。我需要设置URL重写,所以除了给定的异常都将被改写到角的index.html的。

I have an AngularJS webapp and Jersey backend. I need to setup URL rewriting, so everything except given exceptions will be rewritten to Angular's index.html.

例如:

http://my.domain.com/about will be rewritten
https://m.xsw88.com/allimgs/daicuo/20230913/1406.png.jpg will NOT be rewritten (file photo 1 exists)
http://my.domain.com/rest/myservice will be NOT be rewritten (it is a call to REST service)

我已经设置了Tomcat的8 URL重写阀门如下:

I have set up the Tomcat 8 URL Rewrite Valve as follows:

在的conf / server.xml中

<Host name="my.domain.com" appBase="webapps/MyDomainServer" unpackWARs="true"
           autoDeploy="true"
           xmlValidation="false" xmlNamespaceAware="false">
  <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
  <!-- access logging, aliases,...-->
</Host>

在的conf /卡塔利娜/ my.domain.com / rewrite.config

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} ^/rest.*
RewriteRule ^ - [L]

RewriteRule ^ index.html [L]

Tomcat的忽略我重写的设置,没有被改写,没有错误/例外是在日志中。我究竟做错了什么?先谢谢了。

Tomcat ignores my rewrite settings, nothing is rewritten, no error/exception is in the log. What am I doing wrong? Thanks in advance.

我曾试图移动RewriteValve在META-INF到config.xml和重写配置到WEB-INF,但它以同样的方式表现。

I have tried to move RewriteValve to config.xml in META-INF and rewrite config to WEB-INF, but it behaved in the same way.

推荐答案

我已经找到了解决办法,问题是在错误/故障rewrite.config文件。正确的应该是:

I have found the solution, problem was in wrong/faulty rewrite.config file. Correct should be:

RewriteCond %{REQUEST_URI} ^/(css|img|js|partials|rest|favicon).*$
RewriteRule ^.*$ - [L]

RewriteRule ^.*$ /index.html [L,QSA]

在第一行列举不应该被重写的URI。其他将被重写的index.html。

On the first line are enumerated URIs which should not be rewritten. Everything else will be rewritten to index.html.