Forum Moderators: phranque

Message Too Old, No Replies

htaccess with regards to hosting 2 domains on one web space

htaccess rewrite to a specific directory

         

Frequent

2:26 am on Jul 11, 2005 (gmt 0)

10+ Year Member



I have tried some searching and haven't found exactly what I'm looking for so I thought I'd post my question.

The situation is that one of my hosting accounts allows multiple dns but only one "space". The primary site hosted there uses only 5% of the bandwidth and even less of the storage and it never will use more than that by design.

I have a directory set up with content more suited to a "stand alone" site and have secured an appropriate domain name. Rather than set up another hosting account I'd rather pinch pennies and simply utilize the "excess" on the existing account.

In order to prevent dupe content penalties I'd like to use htaccess to direct requests for www.domainA.com to the www.domainA/domainA/ directory and requests coming for www.domainB.com to the www.domainB.com/domainB/ directory.

My searches so far seem to indicate that this is possible but I haven't found exactly how to go about it.

Freq---

Frequent

8:08 pm on Jul 11, 2005 (gmt 0)

10+ Year Member



If I understand things correctly all I need to do is 301 all requests for www.domainA.com to www.domainA.com/domainA/ and www.domainB.com to www.domainB.com/domainB/. I assume this shouldn't be a problem with the htaccess file in the root since both domains point to the same nameserver and the name server has both domains set up on this account.

Could someone with a clue about htaccess (obviously not me) please confirm if this will work. The main site is currently getting spidered heavily so I can't risk taking it down with a major blunder.

Thanks in advance,

Freq---

jdMorgan

9:43 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you sure don't want to do an external (301) redirect to accomplish this. The result would be that both domains would be "exposed" as resolving to a subdirectory, resulting in ugly and unnecessarily-long URLs.

Instead, use mod_rewrite to add the subdirectory name to the requested filepath internal to the server only.

There are a lot of threads here about mapping multiple domains into the filespace of a single domain, so you might want to try a search.

Jim

Frequent

3:24 am on Jul 13, 2005 (gmt 0)

10+ Year Member



jdMorgan,

Other than long "ugly" urls is there any disadvantage to doing as I had originally planned with regards to SEO or risk of a penalty from the engines for not having anything in the root (other than a robots.txt and an htaccess file)?

Thanks for your help. I will continue searching until I find exactly what I need. I'm sure it's in a thread somewhere.

Freq---

jdMorgan

6:14 pm on Jul 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is simply no reason to expose the subdirectories, *and* it makes the URLs ugly, so don't do a redirect. Use an internal rewrite instead. It's very simple code, and nothing to be concerned with SEO-wise.

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteCond $1!^domainA/
RewriteRule (.*) /domainA/$1 [L]
#
RewriteCond %{HTTP_HOST} ^(www\.)?domainB\.com
RewriteCond $1!^domainB/
RewriteRule (.*) /domainB/$1 [L]

Each domain is rewritten to its own subdirectory, unless the request has already been rewritten to the subdirectory (mod_rewrite in .htaccess appears to be recursive, so testing for this case is necessary if the code is in .htaccess, but not if it's in httpd.conf).

Search engines and visitors will be entirely unaware that two sites are hosted under the same account, unless they investigate the domain registrations and DNS records; the rewrite process itself is entirely transparent.

You can move everything, including a robots.txt file for each domain, into its own subdirectory; the only file needed in the home directory is this .htaccess file.

True HTTP/1.0 clients do not provide a host header. For this reason, a default rewrite (to domainA) is provided above. While many clients advertise that they are HTTP/1.0, most have been modified to send the host header, since otherwise they cannot access any sites that are hosted on name-based virtual servers (Name-based servers depend on the HTTP host header to figure out which of the many sites at the same IP address to serve).

Since you've set up the DNS, the only concern is whether your server is set up to accept both domain names and send requests for them to your document root ('home' directory). If that's taken care of, then adding the above rules to your existing .htaccess file should do what you need.

The Apache forum Charter [webmasterworld.com] contains links to basic mod_rewrite documentation; To prevent trouble, look up each of the directives used in the code above, and make sure you understand how it works before trying to use it.

Jim

Frequent

5:01 pm on Jul 14, 2005 (gmt 0)

10+ Year Member



Thanks Jim,

Since my last post I have been sifting through every doc and tutorial I could find.

What I had come up with so far isn't nearly as pretty as what you've provided so I will definately try yours first and just continue refining mine so that I have a fair understanding of what is really going on there.

Just to clarify another point. There is no reason I can't still host one of the sites at root and use the htaccess to re-write the other correct? Is there any conflict there that I'm not aware of?

Thanks again,

Freq---

jdMorgan

7:41 pm on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Just to clarify another point. There is no reason I can't still host one of the sites at root and use the htaccess to re-write the other correct? Is there any conflict there that I'm not aware of?

It's a question of organization and portability. If you "mix" the domain-to-subdirectory .htaccess code with the other .htaccess code for one of the domains, and later decide to move or modify that domain, then you're left with the task of untangling the two files.

On the other hand, if you put each domain in it's own subdirectory, there's no intermixing in .htaccess, and the whole thing is a lot more uncluttered and easier to manage. The entire structure of each domain is then independent of the others, and 'portable' -- to move it to another server, the only work is in copying it.

Jim

Frequent

8:33 pm on Jul 14, 2005 (gmt 0)

10+ Year Member



Excellent point that I had not considered. I'm glad I asked before I got too much farther along that path.

You've been a huge help.

Freq---