.htaccess - redirecting an apache server to tomcat without changing url

Viewed 318

So, I am developing a system in java, the server is tomcat, I packed the java code and the static content (.html, .css, .js) inside the same .war, everything is working 100%, so I can access the system this way:

http://ip:port/domain-tomcat

I also created a domain, let's call it www.example.com, in this service I have the apache server running, apache will actually work as a redirect to my tomcat address, I will not put any static content in his public_html folder, so when I type www.example.com it is redirected to http://ip:port/domain-tomcat

I made the following configuration for this redirect:

I created an .htaccess file that looked like this:

RewriteEngine on
RewriteCond% {HTTP_HOST} ^example.com [NC,OR]
RewriteCond% {HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ Http://ip:port/domain-tomcat$1 [L,R=301,NC]

It is working, the problem is that when the redirect to the tomcat address occurs the tomcat address appears in the url, that is, I type www.example.com and the redirect appears http://ip:port/domain-tomcat

Is there any way to redirect from www.example.com to http://ip:port/domain-tomcat and always keep the address www.example.com?

As I browse the site I would just like the http://ip:port/domain-tomcat not to appear... I wish that when browsing the site it would be www.example.com/page1, www.example.com/page2, there is any way to do that?

1 Answers

Instead of using redirects, I recommend using a connector between Apache and Tomcat called mod_jk. It also allows for setting up load balancing, should that ever become relevant for your app.

Here is how I did it on my Debian stretch box:

Install mod_jk

sudo apt-get install libapache2-mod-jk

The default configuration is probably fine for your needs. It defines a so-called worker named ajp13_worker that will try to connect to Tomcat on localhost, listening on port 8009. If you need to tweak those settings, edit /etc/libapache2-mod-jk/workers.properties accordingly.

Enable mod_jk

sudo a2enmod jk

Configure the virtual host to use the connector

<VirtualHost *:80>
  ...
  JkMount /* ajp13_worker
  ...
</VirtualHost>

Restart Apache

sudo /etc/init.d/apache2 restart

Related