Luis Alvarado
Luis Alvarado

Reputation: 9396

How to redirect multiple domains to different sub directories with Apache

I want to redirect several domains to different sub directories. For example:

If a visitor uses the domain www.cookie.com which has a HTTP redirect to www.mainme.com then when the visitor gets there, Apache detects the domain and redirects it to mainme.com/cookie

Same for puppy.com. If it gets HTTP redirected to mainme.com, when the user gets there it gets redirected to mainme.com/puppy

Can this be done in .htaccess?

Using Linux as server with /var/www as web folder.

Not using Virtualhost right now but learning how to do it.

Upvotes: 4

Views: 5116

Answers (2)

Boann
Boann

Reputation: 50041

I hope I understood you right, but if by "forwarded" you mean an HTTP redirect, then the rest of what you say won't work. There's no way for Apache to detect the domain if you've already discarded that information by redirecting all to just www.mainme.com. (The HTTP referer not reliable because it can be turned off (I have it off) but I'm not sure its meaning with redirects is even what you'd need for this, or if it's consistent across browsers.)

I'm not sure what you mean by "tell the domain seller". Is that people, or just editing the settings yourself in your registrar's control panel? But I guess either way if you can tell them to do one thing you can tell them to do something else, so:

The easiest solution is probably to just redirect to different URLs instead of all to the same URL. I.e., directly to www.mainme.com/sitename. Or if you can only forward to a domain name and not a full URL, make a sub-domain for each: sitename.mainme.com, which could then redirect a second time if you want to get it to www.mainme.com/sitename.

The other way to do it is to set the IP of each domain to your mainme.com server, and just host them all normally as virtual hosts. They could redirect if you want but you could also just serve content directly from cookie/puppy/etc.com.

Upvotes: 0

Seybsen
Seybsen

Reputation: 15580

I don't really know what you mean by

which is forwarded to www.mainme.com

If it get's forwarded with a HTTP-redirect you need to change that redirect to your needs (so attach /cookie or /puppy at the end).

If your domains are sharing the same documentroot you need to place this code in your .htacces in it:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?cookie\.com$
RewriteRule ^(.*)$ http://www.mainme.com/cookie? [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?puppy\.com$
RewriteRule ^(.*)$ http://www.mainme.com/puppy? [R,L]

EDIT: So you might have the http-referer as clue where the user came from. Try this:

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http[s]?://(www\.)?cookie\.com(.*)?$
RewriteRule ^(.*)$ http[s]?://www.mainme.com/cookie? [R,L]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?puppy\.com(.*)?$
RewriteRule ^(.*)$ http://www.mainme.com/puppy? [R,L]

Upvotes: 5

Related Questions