views:

453

answers:

2

I want to setup a system so that multiple custom domains like [mydomain.com/params] will redirect to [myapp.com/mydomain.com/params]. Can I do this using only DNS settings?

I'm guessing that is not possible, so would it be a better solution to direct mydomain1.com, mydomain2.com, mydomain3.com, etc. to one IP address then use Mod_Rewrite to direct each request (invisibly) to myapp.com/mydomain#.com/params ? Each redirected URL leads to content that is loaded from a centrally hosted CMS.

Any suggestions, resources, and/or solutions would be greatly appreciated!

+1  A: 

No, you can't use only DNS for that.

If every domain can run standalone (www.domain.com) this would be a straightforward multi-site setup and does not require mod_rewrite, just a bunch of <virtualHost> directives that point to each site.

If you need exactly the setup you describe (http://www.hostname.com/www.2ndhostname.com/directoryname) you would need one <VirtualHost> with all the domains as aliases, and a mod_rewrite based redirect to point incoming requests to the right directory.

Pekka
Thanks for the quick response. The problem is that this needs to be automatically scalable - meaning that my PHP code needs to be able to create/destroy domain records on-the-fly (sorry, I didn't make that clear).What I might do is have two domains: myapp.com + myapp2.comThen can each domain#.com DNS record be setup to invisibly redirect to domain#.com.myapp2.com? The server with myapp2.com would be setup for wildcard subdomains and would then use Mod_Rewrite to go to myapp.com/domain.com/Sound at all plausible?I'm new to DNS setup and trying to learn...
Stephen Gacka
I'm not entirely sure what you are aiming to do, but it sounds too complicated to my ears. How many servers do you exactly have, and how many do you *need*? Wouln't it be enough to map all domains to one server, and let mod_rewrite sort out the rest?
Pekka
I agree, it seems too complicated. I'm using Rackspace CloudSites to host the central app (it's cheap and allows for on-demand scaling), which unfortunately does not allow for any advanced configuration. I am using Rackspace CloudServers (near-instant deployment, very inexpensive, scalable, root access) to handle the more advanced customization. I've setup a similar config before (wildcard subdomains on a CloudServer redirected to a site on CloudSites) but the DNS issues now add another layer of complexity.
Stephen Gacka
For what you need, the DNS thing isn't really that hard basically. You map a domain to a server. Period. The rest is up to Apache and PHP to handle. You can set up a Apache virtualHost that takes all incoming requests and forwards them to myapp.com/mydomain.com/params no problem. This can also be set up scalable, so that you don't have to set it up for every new domain. Whether programming a solution to add DNS entries is a sensible thing I don't know - it takes manual work to register the domain anyway, and typing in the right IP address should be just one more step in the process.
Pekka
A: 

Here's the solution:

  1. Set DNS Address records for all vanity domains to the same IP address (so d1.com, d2.com, d3.com, etc. all have DNS A records set to one IP or FQDN for example)
  2. Setup the server with one VirtualHost using the IP as the domain
  3. Within that VirtualHost's root directory, create a .htaccess that sets up the mod_rewrite
  4. Use the following for the mod_rewrite in the .htaccess:
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} (.*) [NC]
RewriteRule (.*) http://myapp.com/%1/$1 [P,R=301,L]

%1 = the domain that is requested, so www.d1.com or d1.com
$1 = the rest of the URL that comes after the vanity URL (d1.com/**everyting/else**

This config invisibly redirects all requests.

Examples:

d1.com => returns content from => myapp.com/d1.com/

www.d1.com => returns content from => myapp.com/www.d1.com/

d1.com/blog/post/1 => returns content from => myapp.com/d1.com/post/1
Stephen Gacka