Tomcat 8 URL 重写问题 [英] Tomcat 8 URL Rewrite Issues

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

问题描述

我已经让 tomcat 8 重写工作,但似乎在 rewrite.config 中遗漏了一些导致最后一个条件无法执行的内容.为了他人的利益,我让 RewriteValve 为我的特定应用程序而不是全局工作.下面给出了对我有用的方法.

I have got the tomcat 8 rewrite to work but seems to missing something in rewrite.config that is causing the last condition to not execute. For benefit of others, i have the RewriteValve to work for my specific application and not globally. What works for me is given below.

在我的应用程序的 META-INF context.xml 文件中,我包含在下面的行

In my app's META-INF context.xml file i have included below line

<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" asyncSupported="true"/>

在我的应用程序的 WEB-INF 中,我有一个 rewrite.config 文件,在合并了在另一个 tomcat 8 重写线程中看到的关于使用 {REQUEST_FILENAME} 为空的问题的反馈后,该文件说明了以下内容.我没有使用 REQUEST_FILENAME,我的配置如下所示.

In my app's WEB-INF, i have a rewrite.config file that says below things after incorporating the feedback seen in the other tomcat 8 rewrite thread, regarding issue with using {REQUEST_FILENAME} being null. I have not used the REQUEST_FILENAME and my config looks like below.

RewriteCond %{REQUEST_URI} .*\.(css|js|html|png|jpg|jpeg|gif|txt|ico) [OR]
RewriteCond %{REQUEST_URI} ^\/api\/ [OR] 
RewriteRule ^(.*)$ - 

RewriteRule ^(.*)$ /index.html 

现在,如果 URI 是 js、css 等,或者 URI 以/api/开头,我会看到正在评估重写规则并且没有进行任何替换.即下面的网址似乎可以正常工作,并且没有进行任何替换.本地主机:8080/api/abc/xyz , 本地主机:8080/css/abc.min.css

Now if URI is a js,css etc or if the URI starts with /api/ i see the rewrite rule is being evaluated and no substitution is being done. i.e below urls seem to work ok and no substitution is being done. localhost:8080/api/abc/xyz , localhost:8080/css/abc.min.css

但是由于某种原因,即使 URI 有一个有效的规则可以被它击中,最后一条规则也根本没有被击中.例如.像 localhost:8080/def/ghi 这样的 URL 应该被重定向到 index.html,但它似乎没有被重写.我不确定在 rewrite.config 中我缺少什么导致这种行为.我可以搬到一个!条件也进行重写,但只是想在我使用多重 RewriteRule 组合时让我的理解清晰.

But for some reason the last rule is not getting hit at all even when the URI has a valid one to get hit by it.For ex. URL like localhost:8080/def/ghi should have got redirected to index.html, but it seem to not getting rewritten. I am not sure what am i missing in the rewrite.config causing this behavior. I could move to a ! condition to do the rewrite too, but just want to get my understanding clear when i use Mulitple RewriteRule combination.

感谢任何帮助.

推荐答案

我发现这个问题是因为我遇到了类似的问题.我花了几个小时寻找不需要我将特定文件类型列入白名单的解决方案.最终我反编译了 org.apache.catalina.valves.rewrite.RewriteValve 并找到了答案.

I found this question because I had a similar problem. I spent hours looking for a solution that didn't require me to whitelist specific file types. Eventually I decompiled org.apache.catalina.valves.rewrite.RewriteValve and found the answer.

我的情况类似,但我的 Angular 应用嵌套在较旧的非 Angular 应用中.这意味着它的 URL 类似于 http://localhost:8080/mywebapp/ng/index.html(应用程序的 base-href 因此是/mywebapp/ng").

My case is similar, but my Angular app is nested inside an older non-Angular app. That means that the URL to it someting like http://localhost:8080/mywebapp/ng/index.html (the app's base-href is thus "/mywebapp/ng").

我对使用 REQUEST_URI、REQUEST_FILENAME 或 SCRIPT_FILENAME 的规则不满意.对我有用的是 SERVLET_PATH(不知道为什么).

I had no luck with rules using REQUEST_URI, REQUEST_FILENAME, or SCRIPT_FILENAME. What worked for me was SERVLET_PATH (no idea why).

我最终得到了一个包含这两个文件的解决方案:

I wound up with a solution including these two files:

/META-INF/context.xml:

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
</Context>

/WEB-INF/rewrite.config:

RewriteCond %{SERVLET_PATH} !-f
RewriteRule ^/ng/(.*)$ /ng/index.html [L]

结果是所有不是真实文件的东西都由 Angular 应用程序提供服务.

The result is that everything which is not a real file gets served by the Angular app.

注意:这在 Tomcat 8.0 上工作,其中 AoT 编译的 Angular2 (v4.0.0) 应用程序嵌套在现有的 Web 应用程序中.

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

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