How do I setup a domain to forward to another domain via DNS or apache?
3 Answers
The DNS does not know what a redirect is, all it knows is how to assign a domain to a given ip by either using a Canonical name or an IP.
So the first step is to create an A name for the domain you want to redirect in your DNS to the apache server IP:
Let's say your apache server ip is 10.0.0.1 so in the DNS record of the domain you want to redirect you would need to have the follow entries:
domain.com. IN A 10.0.0.1
www IN CNAME domain.com.
Restart/reload your dns server to make the changes apply.
If the domain you want to redirect is being hosted on the same server, it makes things a lot simple on the apache server, all you need to do is to add the domains on the ServerAlias example:
<VirtualHost *:80>
ServerName mymaindomain.com
ServerAlias myredirecteddomain.com myotherredirecteddomain.com
DocumentRoot /www/domain/public_html
</virtualhost>
If the destination domain is not being hosted on the same server then you can do the follow:
<VirtualHost *:80>
ServerName myredirecteddomain.com
ServerAlias www.myredirecteddomain.com myotherredirecteddomain.com www.myotherredirecteddomain.com
Redirect permanent / http://www.redirecteverythinghere.com/
</virtualhost>
Another option is to use the .htaccess if that given domain is already being redirected to your main domain:
RewriteEngine on
rewritecond %{http_host} ^long-name.com [nc]
rewriterule ^(.*)$ http://www.short-name.com/$1 [r=301,nc]
If the site will serve requests for the domain you want to forward, just create a DNS CNAME record from the forwarded domain to the target domain name.
If the site is a name based virtual host, you may need to also add the forwarded domain name to the VirtualHost section as a ServerAlias.
-
How can I use a cname entry to forward the domain completely? I only want to forward short-domain.com to long-domain.com, ignoring any /index.html or anything past it.– BenAug 28, 2010 at 2:40
If you want to forward the domain, set the DNS A record to forward to the server. You should have an MX record for the domain. It should point to a server which will handle your mail, especially for [email protected].
In apache follow the options Prix gave using short-domain, to do the redirect. The following rewrite ruleset will forward requests from short-domain to long-domain. long-domain.com does not need to be hosted on the same server as short-domain. Use the appropriate domains. If you omit the RewriteCond statement all matching requests will be redirected.
RewriteEngine On RewriteCond %{HTTP_HOST} short-domain.com$ [NC] RewriteRule ^(.*) http://long-domain.com$1 [L,R=301]
These rules would normally be placed in a VirtualHost definition.