Forum Moderators: phranque

Message Too Old, No Replies

Virtual Hosts & mod rewrite

Virtual Hosts | mod_rewrite & Documentroot Problem

         

GWiggs

8:46 pm on Aug 5, 2010 (gmt 0)

10+ Year Member



I have a site that currently runs a jsp and servlet based application. When the request comes in I use rewrite to make sure that the URL is
www.example.com/home/*
.

I am attempting to alter the site to add other virtual hosts for php (CakePHP) applications that I am writing.

I want to differentiate between routings by using the URL. If the URL contains
www.example.com/datamart/*
then I want to avoid passing the request to Tomcat and use the document root established for this new application.


NameVirtualHost *:80

<VirtualHost *>
ServerName www.orcastech.com
rewriteengine on
rewritelog logs/rewrite.log
rewriteloglevel 1

RewriteRule !^/datamart - [C]
RewriteRule !^/emba48wiki - [C]
## If /home context already requested then do nothing
RewriteRule !^/home - [C]
RewriteRule ^/(.*) http://www.orcastech.com/home/$1 [L]

SetEnvIf Request_URI \.php no-jk
SetEnvIf Request_URI \.cgi no-jk
# Use SetEnvIf to st no-jk when /datamart/ is encountered
SetEnvIf Request_URI "/datamart/*" no-jk

JkMount /home* worker1
DocumentRoot "c:\Apache\apache-tomcat-6.0.29\webapps\home"
</VirtualHost>

<VirtualHost *>
ServerPath /datamart/
DocumentRoot "C:\vhosts\datamart\htdocs"
CustomLog logs/system.datamart.access.log combined
ErrorLog logs/system.datamart.com-error.log

</VirtualHost>


Essentially - If the request comes through as either example.com or example.com/home/ then I want to pass the request to Tomcat and use the tomcat document root. If the request comes through as example.com/datamart/* then I want to use the datamart directory.

Can anyone help me with this? When I check the error logs my errors come from "c:\Apache\apache-tomcat-6.0.29\webapps\home" - if things are working correctly, I should be seeing the server attempting to access "C:\vhosts\datamart\htdocs".

jdMorgan

10:06 pm on Aug 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll hazard a guess here, but with no guarantees... The main point being that you cannot have two DocumentRoots in the same vHost. So use mod_alias to pass requests --when needed-- over to Tomcat.

Other minor tweaks are for readability, compactness, and efficiency.

NameVirtualHost *:80
#
<VirtualHost *>
#
ServerName www.orcastech.com
ServerAlias orcastech.com
#
DocumentRoot "c:\vhosts\datamart\htdocs"
CustomLog logs/system.datamart.access.log combined
ErrorLog logs/system.datamart.com-error.log
#
SetEnvIf Request_URI "\.(php|cgi)$" no-jk
# Use SetEnvIf to st no-jk when /datamart/ is encountered
SetEnvIf Request_URI "^/datamart" no-jk
#
RewriteEngine on
RewriteLog logs/rewrite.log
RewriteLoglevel 1
#
RewriteCond $1 !^(datamart|emba48wiki|home)
RewriteRule ^/(.*)$ http://www.orcastech.com/home/$1 [R=301,L]
#
JkMount /home worker1
Alias /home "c:\Apache\apache-tomcat-6.0.29\webapps\home"
#
</VirtualHost>

This probably still needs significant work, but I hope that it demonstrates the idea...

Jim

GWiggs

10:54 pm on Aug 6, 2010 (gmt 0)

10+ Year Member



Jim,

Thanks for the quick reply. I've tried your solution as well as several modifications of your approach but all end in failure.

When I plug in exactly what you've provided above the URL www.example.com/datamart is rewritten to:

http://www.example.com/home/vhosts/datamart/app/webroot/htdocs/datamart


Also, under this same configuration, www.example.com is rewritten correctly to: www.example.com/home/ but I get a 403 error on that directory.

In my httpd.conf file DocumentRoot is set to "C:/htdocs".

I also tried it by creating the alias as C:/vhosts/datamart/htdocs and leaving the tomcat directory as the document root. In this case, I receive the 403 when I attempt to access the 'home' context and my 'datamart' url is still rewritten to:
http://www.orcastech.com/home/vhosts/datamart/app/webroot/htdocs/datamart

which makes no sense to me at all.

I know that this is on the right track, but something isn't working correctly. Any more ideas?

jdMorgan

2:11 pm on Aug 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is most usual to give "DocumentRoot" paths which are relative to the Apache directory. Check (or test) to be sure you really need the full path in this line:
 DocumentRoot "c:\vhosts\datamart\htdocs" 


Jim