views:

625

answers:

2

Hi--

Caveat: I am not an Apache expert or webmaster by training or trade (C++ developer), so I expect this is a fairly obvious, newbie-level question. Apologies in advance.

I need an Apache 2.x rewrite rule that will map a requested domain into our domain as a subdomain.

Simplified Example(s):

domain1.com/index.php?option=80  ->  domain1.masterdomain.com/index.php?option=80
www.domain1.com/index.php?option=99 ->  domain1.masterdomain.com/index.php?option=99
domain2.com/index.php?option=33  ->  domain2.masterdomain.com/index.php?option=33
www.domain2.com/index.php?option=44 ->  domain2.masterdomain.com/index.php?option=44

I've tried a variety of suggested options, but so far, no joy. The latest attempt is:

RewriteRule   ([^.]+)\.com(.*) http://$1.masterdomain.com [L]

Note: this lives in a virtual host that owns port 80 on a specific IP, so nothing else interesting going on in the VHost that I can see having any affect on this.

I believe my problem is all in my regex, but honestly, it's eluding me.

Any assistance would be much apperciated. I've been studying the Apache docs and all the Googled tips I can find, but I'm just not seeing it.

Thanks~

A: 

Actually, there's no need to rewrite the domain name, as long as the "source" and "destination" domains are handled by the same server. You just need to alias the different domains to refer to the same host. Specifically: I suppose that right now you have <VirtualHost> blocks that look something like this:

<VirtualHost *:80>
    ServerName domain1.masterdomain.com
    # other rules
</VirtualHost>
<VirtualHost *:80>
    ServerName domain2.masterdomain.com
    # other rules
</VirtualHost>

All you have to do is add the ServerAlias directives as shown below:

<VirtualHost *:80>
    ServerName domain1.masterdomain.com
    ServerAlias domain1.com
    ServerAlias www.domain1.com
    # same other rules as before
</VirtualHost>
<VirtualHost *:80>
    ServerName domain2.masterdomain.com
    ServerAlias domain2.com
    ServerAlias www.domain2.com
    # same other rules as before
</VirtualHost>

This will make Apache handle the domain names domain1.com and www.domain1.com the same way it handles domain1.masterdomain.com.

David Zaslavsky
A: 

Hi David--

Thanks for the fast reply! Actually, I have a single VHost that's handling all the sites (since they all have the same structure), leveraging VirtualDocumentRoot directives. I can post the whole VHost entry if desired, but I thought that would be overkill for my question.

I have made progress since my previous post. The following executes the rewrite as desired, albeit it updates the browser url, and I'm looking to NOT have the user know they've been redirected internally.

 RewriteCond    %{HTTP_HOST} ^(www\.)?([^.]+)\.com$ [NC]
 RewriteRule    ^(.*)$   http://%2.masterdomain.com$1

I'm not sure why it's updating the browser URL on it's redirect though. I thought I read that only happens if you explicitly add a [R] on the end of the rule line. Ultimately, the browser needs to still display: test1.com/index.php?id=5, while internally the cgi receives test1.masterdomain.com/index.php?id=5.

Another data point that may be helpful (or not), I see in my rewrite.log that, after the match, it's implicitly executing a redirect (rc=302). Here's the two lines I think are important:

 implicitly forcing redirect (rc=302) with http://test1.masterdomain.com/index.php
 redirect to http://test1.masterdomain.com/index.php?id=5 [REDIRECT/302]

Any thoughts/suggestions are greatly appreciated! 20+ years C++, but just under a week with Apache, so still a very struggling newbie.

-wb

P.S. due to some coding requirements for the web apps that are locked, I actually need the url received by the cgi to be xxx.masterdomain.com (long story, not my choice or approach; just trying to solve the problem I've been given with it's highly constrained requirements.... Also, my apologies for the sanitized data-- following directives from on-high. -smile-) Thanks again!