apache – End slash error with mod_rewrite and mod_jk

Question:

I have the following structure on my server.

  • Apache HTTPD (port 80)
  • Tomcat (port 8080)

I used mod_jk to make the connection between apache and tomcat and it is working perfectly.

I created a subdomain to point to an application that is on tomcat. And I made the following configuration of VirtualHost.

<VirtualHost *:80>
    ServerName www.cardapio.dreamt.com.br
    ServerAlias cardapio.dreamt.com.br
    JkMount /cardapio/* worker1
    RewriteEngine On
    RewriteRule ^/(.*) /cardapio/$1 [L,PT]
</VirtualHost>

However, and I ran into two problems:

  1. When accessing cardapio.dreamt.com.br/dreamtech/ (with the slash at the end) the application runs without any problem. And when I access cardapio.dreamt.com.br/dreamtech (without the final slash) I am redirected to an invalid URL cardapio.dreamt.com.br/cardapio/dreamtech/ .
  2. By accessing cardapio.dreamt.com.br/ my application works because it is not trying to get the JS and CSS cardapio.dreamt.com.br/ Cardapio /xxx.js (for example). And I can't change it because these imports are made automatically by JSF.

How to solve these problems?

Answer:

The following rewrite rule adds a slash to the end specifically in the case of accessing the /dreamtech path:

RewriteRule ^(dreamtech)$ $1/ [R]

Try to put it like this:

<VirtualHost *:80>
    ServerName www.cardapio.dreamt.com.br
    ServerAlias cardapio.dreamt.com.br
    JkMount /cardapio/* worker1
    RewriteEngine On
    RewriteRule ^(dreamtech)$ $1/ [R]
    RewriteRule ^/(.*) /cardapio/$1 [L,PT]
</VirtualHost>

Apache's own guide gives some tips on this bar issue and recommends this kind of approach. See here .

Previously, I had tried using the generic rewrite condition to check if the URL was for a file, but that wouldn't really work since you're connecting to a Tomcat.

You can do some testing of the rewrite module here or here .

Scroll to Top