How to use mod_rewrite with Apache -> mod_jk -> tomcat setup?
Place this snippet right after last jkmount line in your apache config:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} !^www\.example\.name$ [NC]
RewriteRule ^ https://www.example.name%{REQUEST_URI} [NE,L,R=301]
</IfModule>
What this rule is doing is IF scheme is https and your http host is NOT www.mydaomain.com
THEN redirect request https://example.com/foo
to https://www.example.com/foo
with a 301 http status.
Nick Foote
Updated on June 05, 2022Comments
-
Nick Foote almost 2 years
Related to some of my earlier questions.
I now have a setup I quite like;
Apache httpd listening on port 80 accepting http and https connections. Several Tomcat instances running on several AJP ports.
Mod_Jk is sending different url requests to different tomcat instances;
www.mydomain.com/demo -> tomcat:8101 www.mydomain.com/test -> tomcat:8102 www.mydomain.com/ -> tomcat:8100
This is achieved with the following config in httpd.conf (or included sub files);
LoadModule jk_module modules/mod_jk.so JkWorkersFile conf/workers.properties JkLogFile logs/mod_jk.log JkLogLevel info NameVirtualHost *:80 <VirtualHost *:80> JkMount /demo* demoTomcat (workers.properties not shown) JkMount /test* testTomcat JkMount /* rootTomcat </VirtualHost>
And this all works great. I also have SSL setup and running for https connections using a similar VirtualHost tag;
<VirtualHost _default_:443> JkMount /demo* demoTomcat JkMount /test* testTomcat JkMount /* rootTomcat ... SSL Stuff follows ....
What I'm now having trouble is that my SSL Cert is only for www.mydomain.com and NOT mydomain.com.
I've been advised to use the follow mod_rewrite calls;
Options +FollowSymlinks LoadModule rewrite_module modules/mod_rewrite.so RewriteEngine On RewriteCond %{HTTP_HOST} !^(www\.|$) [NC] RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [PT,L]
I've placed these before before and after the mod_jk rules in the httpd.conf file. Apache was at first complaining that RewriteEngine was an invalid command, but that went away when I remembered the LoadModule command first :) Now Apache restarts just fine, the server starts and accepts requests and everything works the way it use to ... but thats just it, these mod_rewrite commands appear to have no effect?
I type
http://mydomain.com
into the browser and I just get my website as per normal. The url does not appear to change tohttp://www.mydomain.com
and when I start to access the secured areas I get warnings thatmydomain.com
is NOT secured and is serving me a cert from some other website calledwww.mydomain.com
(why this is an issue at all and it can't just use some logic to realise its the same site, I don't know!).Am I placing the mod_rewrite rules in the wrong place? I've read that it should work, the rewrites should change the url to www. and then pass through to the mod_jk stuff for anything further?
-
Nick Foote about 13 yearsHi, no it didnt. I've tried that snippet inside and outside of the VirtualHost tags, both the vhost ones and the ssl ones. I've tried before and after the JkMount lines. Nothing seems to happen, except maybe the browser hangs where before it straight away complained of the invalid cert! I've set the rewrite log level to 9! The log files gets created (so the module must be working) but nothing EVER gets logged! 0 bytes all the time!
-
Nick Foote about 13 yearsI don't know what I'm missing here. I've even tried someones suggested rule that just redirects from a www.mydomain.com/foo.html to my home page and it does nothing. And nothing is ever logged. As far as I can see mod_rewrite is NEVER firing and does nothing but create an empty log file!
-
anubhava about 13 years@Nick Foote: Since this rule is ONLY for https so place for above rules was only before the end of
<VirtualHost _default_:443>
tag. Also can you try replacing this lineRewriteCond %{HTTPS} =on
with this line in my answer above:RewriteCond {SERVER_PORT} =443
-
Nick Foote almost 13 yearseventually got it working but with these; RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC] RewriteRule ^(.*)$ domain.com/$1 [R=301,L]
-
anubhava almost 13 years@Nick Foote: Glad that you got it right, yes I missed
!
in my RewriteCond. -
mmuller over 6 yearsI tried to edit your answer to fix the missing
!
character, but SO complains about the use ofmydomain.com
... Didn't want to alter your answer so much by replacing the domains (SO suggests using example.com) considering that it actually answers OP's question accurately...