我有一个 AngularJS 网络应用程序和 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 忽略了我的重写设置,没有任何内容被重写,日志中没有错误/异常。我究竟做错了什么?提前致谢。
我曾尝试将 RewriteValve 移动到 META-INF 中的 config.xml 并将配置重写为 WEB-INF,但它的行为方式相同。
最佳答案
我找到了解决方案,问题出在错误/错误的 rewrite.config 文件中。 正确的应该是:
RewriteCond %{REQUEST_URI} ^/(css|img|js|partials|rest|favicon).*$
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ /index.html [L,QSA]
第一行是不应重写的枚举 URI。其他所有内容都将重写为 index.html。
关于java - Tomcat 8 URL 重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28767585/