Tomcat 8 URL 重写 [英] Tomcat 8 URL Rewrite

查看:53
本文介绍了Tomcat 8 URL 重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 AngularJS 网络应用程序和 Jersey 后端.我需要设置 URL 重写,所以除了给定的异常之外的所有内容都将被重写为 Angular 的 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
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:

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/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 忽略我的重写设置,没有重写任何内容,日志中也没有错误/异常.我究竟做错了什么?提前致谢.

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.

这篇关于Tomcat 8 URL 重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆