You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David Crane <da...@donorschoose.org> on 2009/06/26 20:47:15 UTC

Combining Apache RewriteRule and JkMount to pass through to Tomcat

I am having difficulty using the internal redirect capabilities of
Apache mod_rewrite and passing through requests to Tomcat through
mod_jk.  We are using Apache/2.2.3 and Tomcat/6.0.16.  Our tomcat war
application is based on SpringFramework 2.5.4, configured with a
SimpleUrlHandlerMapping.  

A concrete example.  We used to serve a 302 response for /crate, so the
client is redirected to a subsequent request for
/donors/redeem_gift_certificate.html?partner=CRATEANDBARREL&utm_campaign
=CBGC.  Now we want our Apache/Tomcat to serve up the content for the
longer URL directly when the short /crate HTTP request arrives.  I keep
getting a 404 error instead.

This is the mod_rewrite directive:

  RewriteRule /crate
/donors/redeem_gift_certificate.html?partner=CRATEANDBARREL&utm_campaign
=CBGC [NC,PT]

I also have this mod_jk directive, which would act on either version of
the URL:

  JkMount /* worker1

First, note that requests to http://www.myserver.com/
donors/redeem_gift_certificate.html?partner=CRATEANDBARREL&utm_campaign=
CBGC work correctly.

However, if I load http://www.myserver.com/crate in a browser, I am
getting a 404 error back from Tomcat and Apache.  It appears that Tomcat
is being passed a mixed-up URL of
"/crate?partner=CRATEANDBARREL&utm_campaign=CBGC".  Our Tomcat
application doesn't have an SimpleUrlHandlerMapping entry for /crate,
but it does for /donors/redeem_gift_certificate.html.  I tested and
found that the HttpServletRequest returns "/crate" for
request.getRequestURI(), but "partner=CRATEANDBARREL&utm_campaign=CBGC"
for request.getQueryString().

Note that I do have the PT flag for the RewriteRule, as others have
advised.  I get the same results by reversing the order of the
RewriteRule and JkMount directives.

This is part of an SEO effort, in which we are trying to persist these
URLs as they were originally requested, rather than do 301/302 redirects
back to the browser or crawler.  So I suspect someone else has figured
this out.

Thanks,
David Crane
davidc@donorschoose.org

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Combining Apache RewriteRule and JkMount to pass through to Tomcat

Posted by André Warnier <aw...@ice-sa.com>.
David Crane wrote:
...
As has been pointed out several times on this forum, if the question is 
just to serve some static content along with the dynamic Java-produced 
content, Tomcat is comparable to Apache httpd as a pure webserver.

If you need to rewrite some URLs nevertheless, you should have a look at 
the following Tomcat servlet filter, which achieves under Tomcat most if 
not all of what mod_rewrite does under Apache :
http://tuckey.org/urlrewrite/

---
But for now, let's take as a given that you need an httpd in front of 
Tomcat.
You may or may not be aware of the following syntax under Apache, which 
is an alternative to the JkMount/JkUnmount directives :

<Location /anyurl>
   SetHandler jakarta-servlet
   SetEnvIf (some condition) no-jk
   ...
</Location>

Full description here :
http://tomcat.apache.org/connectors-doc/reference/apache.html
(at the very bottom of the page)

You can use any Location or LocationMatch sections with this.
The SetHandler does basically the same as what a "JkMount *" would do in 
this case, while the "SetEnvIf no-jk" does the same as JkUnmount (but 
with a lot more flexibility).
E.g.
SetEnvIf REQUEST_URI "\.(gif|jpg|css|js)$" no-jk


I personally prefer that form, because it fits better with the usual 
syntax of Apache httpd, and I find it easier to combine with other 
Apache directives like RewriteRule, SetEnvIf etc...
It's also easier to figure out when exactly things happen.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Combining Apache RewriteRule and JkMount to pass through to Tomcat

Posted by David Crane <da...@donorschoose.org>.
David Crane wrote:
...
> 
> I also have this mod_jk directive, which would act on either version of
> the URL:
> 
>   JkMount /* worker1
> 

André Warnier wrote:
> Considering the above directive, then why do you have, at all :
> - an Apache httpd in front of your Tomcat ?  Since you are attempting to 
> pass everything to Tomcat anyway, why not save yourself the 
> complication, remove httpd, and have Tomcat listening directly on port 
> 80 with it's HTTP Connector ?
> 

I actually do have JkUnMount directives, but I hadn't listed them in my original post, since I doubt they contributed to the problem.  They look like this:

    JkMount /* worker1
    JkUnMount /images/* worker1
    JkUnMount /js/* worker1
    ...

> - and if you have some reason to keep an Apache httpd in front, then 
> with the above directive you do not need your RewriteRule.
> Just let the original URL go through.

I'd prefer to use RewriteRule so that these "vanity URLs" are decoupled from the application itself.  That /crate URL is one of many that mount point into /donors/redeem_gift_certificate.html.  The same pattern happens for quite a few of our pages.  It's nice not to have to do a full application release just to add another of these vanity URLs,.

Thanks,
David Crane

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Combining Apache RewriteRule and JkMount to pass through to Tomcat

Posted by André Warnier <aw...@ice-sa.com>.
David Crane wrote:
...
> 
> I also have this mod_jk directive, which would act on either version of
> the URL:
> 
>   JkMount /* worker1
> 
Considering the above directive, then why do you have, at all :
- an Apache httpd in front of your Tomcat ?  Since you are attempting to 
pass everything to Tomcat anyway, why not save yourself the 
complication, remove httpd, and have Tomcat listening directly on port 
80 with it's HTTP Connector ?

- and if you have some reason to keep an Apache httpd in front, then 
with the above directive you do not need your RewriteRule.
Just let the original URL go through.



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org