I suppose you can check the referer:
RewriteEngine On
RewriteCond %{HTTP_REFERER} /site1/
RewriteCond %{REQUEST_URI} !^/site1/
RewriteRule ^(.*)$ /site1/$1 [L]
RewriteCond %{HTTP_REFERER} /site2/
RewriteCond %{REQUEST_URI} !^/site2/
RewriteRule ^(.*)$ /site2/$1 [L]
This doesn't redirect the browser, so there's a possibility that the browser will cache a "/style.css" and the two sites get mixed, so you may want to redirect the browser instead by changing the flags from [L]
to [L,R]
.