Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite: forwarding multiple domains to another

forwarding multiple domains to another domain

         

UniquelyAm

9:52 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



I've banged my head all night with this and tried pages on top of pages of Google's search results of different ways to accomplish this.

I have 25 domains which 20 of them need to forward to one domain and the other 3 need to forward to another domain and 2 to yet another domain.
Also, I would like domain1.com as well as www.domain1.com to go to the other domain.

I have, for example:

www.domain1.com
www.domain2.com
www.domain3.com

I want those forwarded with mod_rewrite to: www.bigwebsite.com

Then I have:

www.domain4.com
www.domain5.com
www.domain6.com

and I want those forwarded with mod_rewrite to: www.mediumwebsite.com

then I have

www.domain7.com
www.domain8.com

and I want those forwarded with mod_rewrite to: www.smallwebsite.com

-----------------

This is where it gets tricky.

In my httpd.conf, I have my root www directory at:
/home/httpd/

because I have all of MY websites there.
The clients websites are in their home directories, so I create a name-based virtual host container for each of them.

<VirtualHost *:80>
ServerName domain1.com
ServerAlias *.domain1.com
DocumentRoot /home/client/www/domain1
ServerAdmin webmaster@domain1.com
</VirtualHost>

etc., etc.

The domains that I want forwarded and the domains I want them forwarded to are in (example):
/home/client/www/domain1
/home/client/www/domain2
/home/client/www/domain3
/home/client/www/domain4
/home/client/www/domain5
/home/client/www/domain6
/home/client/www/domain7
/home/client/www/domain8
/home/client/www/smallwebsite
/home/client/www/mediumwebsite
/home/client/www/bigwebsite

These are all, like I mentioned above, in the httpd.conf file as name-based virtual hosts.

So yeah... it's had me pretty stumped as to how to go about doing this. I don't even know what I'm supposed to have in httpd.conf and if I need something in an .htaccess file in the /home/client/www/ directories... so any reply that is as "n00bish" as you can make it would be greatly appreciated...

[edited by: UniquelyAm at 9:54 pm (utc) on Aug. 30, 2007]

UniquelyAm

5:46 am on Aug 31, 2007 (gmt 0)

10+ Year Member



I added this to the .htaccess file in the directory that I wanted to forward from.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^w*\.domain1\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^*\.domain1\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC,OR]
RewriteRule ^(.*)$ http://www.bigwebsite.com/$1 [R=301,L]

Well I tried this at first but ended up messing around a bit more and discovered that this, below, works for www and no www, as well.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.bigwebsite.com/$1 [R=301,L]

So I guess I don't need anyones help after all. Thanks anyway.

Amber.

[edited by: jdMorgan at 1:41 pm (utc) on Aug. 31, 2007]
[edit reason] De-linked [/edit]

jdMorgan

1:41 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteEngine On
RewriteCond %{HTTP_HOST} ^\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.bigwebsite.com/$1 [R=301,L]

I'm surprised that you say this works, because it shouldn't. The RewriteCond pattern will only match a request for a hostname of exactly ".domain1.com", which is an invalid hostname because of the leading dot.

I'd suggest:


RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

That will match requests for "example1.com", "www.example1.com", or "example1.com:80" (all perfectly valid) and redirect them to www.example.com

You could also put the code into example1's <VirtualHost> container in httpd.conf, with a small change:


RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com [NC]
RewriteRule [b]^/([/b].*)$ http://www.example.com/$1 [R=301,L]

The URL-path seen by RewriteRule in httpd.conf will include the full URL-path, whereas in .htaccess, it will be stripped of the path-prefix to the directory in which that .htaccess file resides. In other words, there won't be a slash on the URL-path seen by a rule in example.com/.htaccess

Sorry you didn't get an immediate response to your thread; Participation in this forum is limited, and contributors are few.

Jim

UniquelyAm

7:19 pm on Sep 2, 2007 (gmt 0)

10+ Year Member



jd,

I don't know why I thought about stopping back by this thread, but I'm glad I did now.
Thank you so very much for the response!

I pasted the wrong code. I tried the one I pasted before I tried this one:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain1\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteRule ^(.*)$ [bigsite.com...] [R=301,L]

The other one DID work, so I added the code to the 25 domains directories .htaccess files. Then I tried it again and it stopped working! So I'm guessing something was cached. But I rewrote it to what I just posted, went back through 25 domains .htaccess files... again :( ... and tried it and this definitely works for both www. and without.
It's still redirecting to the page I want them to this very minute.

BUT, I like your idea better. I would really like having one code added to one location ( the virtual containers in the httpd.conf file ) instead of 25 different .htaccess files in 25 different directories...
I'm definitely going to try your idea. Plus I'd get an extra variation covered ( the example1.com:80 ).

Anyway, thanks again, jd, very much. I'm just glad to get a helpful response at all.

Take care and best wishes,
Amber.
UniquelyAm

p.s. Apologies for throwing the .com after my user name, jd.
I normally add my user name, UniquelyAm, after my real name and was thinking about my site at the time and added the .com to the end of my user name, but really didn't mean to.
-True story-
I mean I even went to the trouble of taking the domains out of the urls I posted throughout this thread and making them domain1, etc., instead of leaving them what they actually were.

Again, thanks jd, for everything. I'm going to try your suggestion after I get a few hours of sleep.
Take care ~ am.