You will probably want to use the RewriteCond directive examining the server variable %{HTTP_HOST} to match the names of your various domains, and then if the name matches, do an internal rewrite --not a redirect-- to the subdirectory where that domain's files are located.
If your code is in .htaccess, you will also need to test the requested URL to make sure that you have not previously rewritten the request to the subdirectory. Without this test, your .htaccess rules will loop recursively, until the server or client reaches its maximum redirection limit.
As a simple example, consider:
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com
RewriteCond $1 !domain1/
RewriteRule (.*) /domain1/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com
RewriteCond $1 !domain2/
RewriteRule (.*) /domain2/$1 [L]
This approach is workable for just a few domains, but gets unwieldy for more than a half-dozen or so.
For more than just a few domain-subdirectories, it is best to 'tag' the subdirectories with a common prefix to simplify the anti-looping prevention, and to use back-references to carry the name of the domain into the last part of subdirectory name. Something like:
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1omain2omain3)\.com [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?(domain4omain5)\.net
RewriteCond $1 !dsd_
RewriteRule (.*) /dsd_%2/$1 [L]
Here, the we use "dsd_" as a 'tag' to identify all domain-related subdirectories, so that the rewrite looping that I described can be prevented; In this example, a request for the page "foo.html" in the domain "widgets.com" (with or without the leading "www.") will be internally rewritten to "/dsd_widgets/foo.html".
Local variable $1 back-references the requested local URL-path (the "filename"), while local variable %2 back-references the requested hostname. Variable %1, referencing the optional "www.", is not used in this example.
Obviously, there are many possible variations on what you might want to do, so these are just two examples to get you started.
Important: Replace all broken pipe "?quot; characters above with solid pipe characters from your keyboard before trying to use this code; Posting on this forum modifies the pipe characters.
For more information, see the documents cited in our forum charter and the tutorials in the Apache forum section of the WebmasterWorld library.
Once you get your code working, then we can discuss how to prevent "/dsd_widgets/foo.html" from showing up in the search engines... :)
|