简体   繁体   English

重写规则,附加webapp上下文路径与 <spring:url> 和 <c:url> 标签库

[英]Rewrite Rule appending webapp context path conflicts with <spring:url> and <c:url> taglibs

I've got a MV webapp that is using <c:url> (or <spring:url> ) jsp tags to generate links in the view. 我有一个MV webapp,它使用<c:url> (或<spring:url> )jsp标记在视图中生成链接。

My webapp is currently deployed with context path "/" so it is reachable at the address http://mydomain . 我的webapp当前使用上下文路径“ /”部署,因此可以通过地址http://mydomain

Now I want to deploy this app at with a contextpath /app . 现在,我想使用contextpath /app将其部署在。

The new URL to reach the app is now http://mydomain/app . 到达应用程序的新URL现在为http://mydomain/app

I don't like web users to see the contextPath under which the app is deployed and to keep the URL clean. 我不希望网络用户看到部署应用程序的contextPath并保持URL干净。 I'm now using a RewriteRule to append /app to each request that goes through Apache before hitting Tomcat. 我现在使用RewriteRule将/ app附加到通过Apache到达Apache之前的每个请求。

So the user can browse to http://mydomain and the rewrite rule will forward the request to the correct webapp deployed under the contextpath /app . 因此,用户可以浏览到http://mydomain ,并且重写规则会将请求转发到部署在contextpath /app下的正确Webapp。

The problem I've now is that using <c:url> the view generates page links in the HTML that contain the contextPath. 我现在遇到的问题是,使用<c:url>视图会在HTML中生成包含contextPath的页面链接。 I'm trying to hide with the Rewrite Rule. 我正在尝试使用“重写规则”隐藏。

So, after deploying the webapp under the /app context the HTML contains links like http://mydomain/app/other/careers.html instead of http://mydomain/other/careers.html 因此,在/app上下文下部署webapp之后,HTML包含诸如http://mydomain/app/other/careers.html类的链接,而不是http://mydomain/other/careers.html

Is there a way to fix this without having to modify all the pages using <c:url> ? 有没有一种解决方法,而不必使用<c:url>修改所有页面?

The purpose of the taglib is exactly to manage the contextPath so that it is not hardcoded in the pages. taglib的目的完全是为了管理contextPath,以便它不会在页面中进行硬编码。 But my problem is that when I use a Rewrite Rule engine then I don't need this context path in the HTML pages anymore since I'm kind of dealing with it in the Rewrite Rule itself. 但是我的问题是,当我使用重写规则引擎时,我不再需要HTML页面中的此上下文路径,因为我有点在重写规则本身中处理它。

You may want to check out the UrlRewriteFilter at http://tuckey.org/urlrewrite/ 您可能想在http://tuckey.org/urlrewrite/处查看UrlRewriteFilter。

Using this project you can manipluate both inbound and outbound links, the latter one is done via modifying the result of the calls that c:url does behind the scenes to determine how to build the URL. 使用此项目,您可以操作入站出站链接,后一个链接是通过修改c:url在后台进行的调用结果以确定如何构建URL来完成的。

The documentation of the current version seems to be a little bit puzzling, but <outboud-rule> ( https://cdn.rawgit.com/paultuckey/urlrewritefilter/master/src/doc/manual/4.0/index.html#outbound-rule ) element is what you're looking for to automatically modify all page links. 当前版本的文档似乎有些令人费解,但是<outboud-rule>https://cdn.rawgit.com/paultuckey/urlrewritefilter/master/src/doc/manual/4.0/index.html#outbound -rule )元素是您要寻找的用于自动修改所有页面链接的元素。

If you are only using c:url for all your links, then you just need to add the UrlRewriteFilter without the need to change a single page. 如果仅对所有链接使用c:url ,则只需添加UrlRewriteFilter,而无需更改单个页面。

Depending on your overall setup the UrlRewriteFilter can also handle the incoming rewrite (which is configured independently of the outbound links), so there might not be any need for mod_rewrite. 根据您的总体设置,UrlRewriteFilter也可以处理传入的重写(独立于出站链接进行配置),因此可能不需要mod_rewrite。

Hope that this helps. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM