Forum Moderators: phranque

Message Too Old, No Replies

multiple domains using .htaccess

         

justkevin

9:13 pm on Aug 1, 2010 (gmt 0)

10+ Year Member



First of all I would like to say that I really did try to solve this problem reading all sorts of forum posts and help topic. The truth is that all the fixes I found didn't work on me. And usually I'm not asking question when I can find the answer myself, but this is somewhat over my head I'm afraid.

So.. my problem is trying to setting up multiple websites from the same host/server. I have one hosting account and multiple (five) domains. The host is for primary domain, main.com. Further, I would like host domain2.com from a subdomain sub.main.com

When I log in via FTP I have the following structure: domains/main.com/htdocs/sub (or www for main.com). I placed my .htaccess in the 'htdocs' folder, the same folder which holds the 'sub'-subdomain. When there is no .htaccess on the server, domain2.com shows the content of www.main.com

My .htaccess file contained somewhat like:
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} domain2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/vhosts/sub/.*$
RewriteRule ^(.*)$ /vhosts/sub/$1 [L]


This gets me the next error message:
"Not Found. The requested URL /vhosts/sub/www/ was not found on this server."

So the rewriting is allowed but doesn't work. I have tried several versions of this code. And hopefully I have giving all necessary info and hopefully someone can help me. Thanks in advance for reading :)

jdMorgan

1:41 pm on Aug 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't spot a definite problem, partly because your verbal description of what you want to do does not agree with the code or with the example URLs. I see no mention of a subdomain in the code, only a subdirectory... not the same thing at all. Also, the "vhosts" path-part seems to have been lost somewhere.

However, try, replacing that code with something simple, like
 RewriteRule ^ http://www.google.com [R=301,L] 

just to see if it executes. If so, then you can proceed.

Based on what I do see, I'd write the code as:

RewriteEngine on
Options +FollowSymlinks
#
# Internally rewrite requests for URL <anything or nothing>.domain2.com/<URL-path>
# to filepath /sub/<URL-path>
RewriteCond %{HTTP_HOST} ^([^.]+\.)*domain2\.com$ [NC]
RewriteCond $1 !^sub/
RewriteRule ^(.*)$ /sub/$1 [L]

to fix anchoring problems and get rid of wasted CPU cycles.

Also, you could (and should consider) naming your subdirectories based on the domain names, so that the mapping is direct, and you only need one rewriterule to handle all five domains.

Domain ..... Filepath
main.com -> /domains/main.com/
domain2.com -> /domains/domain2.com/
domain3.net -> /domains/domain3.net/
etc.

RewriteEngine on
Options +FollowSymlinks
#
# Internally rewrite requests for URL http://<anything or nothing>.<domain-name>/<URL-path>
# to filepath /domains/<domain-name>/<URL-path>
RewriteCond $1 !^domains/
RewriteCond %{HTTP_HOST} ^([^.]+\.)*([a-z[0-9]\-]+\.[a-z]{3,6})$
RewriteRule ^(.*)$ /domains/%2/$1 [L]

This would yield the 'flat' and consistent domain-to-subdirectory-filespace mapping shown in the chart above.

Jim