2

I'm using Apache 2.4 with mod_jk and Tomcat running a Java servlet. The application I'm serving has an ugly index URL: accessing www.mydomain.com/ takes the user to www.mydomain.com/view/user/www/. I would like to alias this so that users see www.mydomain.com/app/ instead, and I'm trying to use mod_rewrite to achieve this. This is the current setup I have:

LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
JkWorkersFile /etc/apache2/workers.properties
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories # Is it one of these options?

VirtualHost *:80>
        ServerName www.mydomain.com
        Redirect permanent / https:/www.mydomain.com
</VirtualHost>

<VirtualHost *:443>
        ServerName www.mydomain.com
        ServerAdmin webmaster@localhost
        JkMount / tomcat
        JkMount /* tomcat

        LoadModule rewrite_module modules/mod_rewrite.so
        RewriteEngine On
        RewriteRule ^/$ /app [PT]
        RewriteRule ^/app/?$ /view/user/www [PT,L]
        RewriteRule ^/app/(.*)$ /view/user/www/$1 [PT,L]

        SSLEngine on
        SSLCertificateFile /path/to/my_domain.crt
        SSLCertificateKeyFile /path/to/my_domain.key
        SSLCertificateChainFile /path/to/chainfile.crt
        SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
</VirtualHost>

All of this is in apache2.conf. It's currently doing something different to what I would like. When you type in www.mydomain.com/app, the address in the bar changes to www.mydomain.com/view/user/www (and it serves the correct page). I want the bar to continue to display www.mydomain.com/app but display what tomcat has at /view/user/www, thus hiding the ugly URL from the user. How can I achieve this?

PoolOfPeas
  • 383
  • 2
  • 11

2 Answers2

1

Try this:

RewriteRule ^/$ /app [R]
RewriteRule ^/app/?$ /view/user/www [PT]
RewriteRule ^/app/(.*)$ /view/user/www/$1 [PT]

[R] is a redirection, the url should be modified in the address bar.

[PT,L] is not useful since [PT] includes [L].

Lambic
  • 145
  • 6
  • It has the same behaviour as before unfortunately. Typing in www.mydomain.com/app changes the address bar to www.mydomain.com/view/user/www. – PoolOfPeas Dec 20 '15 at 10:36
  • Strange... it's not acting this way in my Apache 2.2.31. I tested it in a .htaccess at my base directory (without the starting slashes) : RewriteEngine on RewriteBase / RewriteRule ^*$ /app [R] RewriteRule ^app/?$ /view/user/www [PT] RewriteRule ^app/(.*)$ /view/user/www/$1 [PT] – Lambic Dec 20 '15 at 10:53
  • I think it has something to do with the fact that it is being served by mod_jk and doesn't refer to a filesystem location? – PoolOfPeas Dec 20 '15 at 10:56
  • OK I think I got it. You don't use RewriteBase, and you should because /app doesn't exist on the server. https://httpd.apache.org/docs/current/en/mod/mod_rewrite.html#rewritebase - or create /app and look what happens. – Lambic Dec 20 '15 at 11:01
  • RewriteBase is n/a because the substitutions are not relative – covener Dec 27 '15 at 16:54
1

RewriteRule ... [PT] + mod_jk requires

JkOptions     +ForwardURICompatUnparsed
covener
  • 17,402
  • 2
  • 31
  • 45