简体   繁体   中英

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.

My webapp is currently deployed with context path "/" so it is reachable at the address http://mydomain .

Now I want to deploy this app at with a contextpath /app .

The new URL to reach the app is now 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. I'm now using a RewriteRule to append /app to each request that goes through Apache before hitting Tomcat.

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 .

The problem I've now is that using <c:url> the view generates page links in the HTML that contain the 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

Is there a way to fix this without having to modify all the pages using <c:url> ?

The purpose of the taglib is exactly to manage the contextPath so that it is not hardcoded in the pages. 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.

You may want to check out the UrlRewriteFilter at http://tuckey.org/urlrewrite/

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.

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.

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.

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.

Hope that this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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