Tomcat 8 URL 重写

Tomcat 8 URL Rewrite

我有一个 AngularJS webapp 和 Jersey 后端。我需要设置 URL 重写,所以除了给定的异常之外的所有内容都将被重写为 Angular 的 index.html。

例如:

http://my.domain.com/about will be rewritten
http://my.domain.com/photos/photo1.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 Rewrite Valve 如下:

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/Catalina/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 忽略我的重写设置,没有任何内容被重写,日志中没有 error/exception。我究竟做错了什么?提前致谢。

我尝试将 RewriteValve 移动到 META-INF 中的 config.xml 并将配置重写为 WEB-INF,但它的行为方式相同。

这是否部署为 java 网络应用程序 (WAR)?您可以在 web.xml:

中实现它
<servlet>
   <servlet-name>index</servlet-name>
   <jsp-file>/index.html</jsp-file>
</servlet>

<servlet-mapping>
   <servlet-name>index</servlet-name>
   <url-pattern>/</url-pattern>
   <url-pattern>/about</url-pattern>
   .. as many as you need ..
<servlet-mapping>

我找到了解决方案,问题在 wrong/faulty rewrite.config 文件中。 正确的应该是:

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

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

第一行列出了不应重写的 URI。其他所有内容都将重写为 index.html。

我无法让它与 REQUEST_URI 一起使用,而且我不喜欢将特定文件列入白名单,所以我在 slightly different manner.[=11= 中解决了它]