28

For example, my domain is example.com and when someone navigates to example.com/sample/page.htm, I want it to redirect to test.com/sample/page.htm.

I know that this can probably easily be done using a .htaccess file or a 404 page. However, I am looking for a solution that doesn't require me to have my DNS provider host any files for me.

I want to do this with DNS settings only, e.g.:

  • CNAME records
  • A records
  • 301 forwarding
  • (any other DNS settings I'm missing)

Is there any way to do this?

3
  • I think what you wanted was more of a wildcard setup, but I don't think it can be implemented at this point without redirects.
    – Tim Post
    Commented Jul 21, 2010 at 23:37
  • can anyone give a clear and simple reply about this....:(
    – user6472
    Commented Mar 31, 2011 at 13:56
  • @fatty, what don't you understand?
    – John Conde
    Commented Mar 31, 2011 at 14:06

4 Answers 4

11

You could make example.com/test.com resolve to the same webserver (IP) via dns (CNAME if you like) and that would work.

If you are looking to do this on a per-path basis than no. DNS is ignorant of things like URL. DNS is simply translating the host part of the url (example.com) into IP address. The web server is in charge of figuring out what (or where) the url is supposed to resolve. I believe you would need to do this at the webserver (http) level.

1
  • 1
    Yeah, if you set up the CNAME on example.com, you don't have to host anything for example.com. Just set the test.com host to respond for example.com, then use mod_rewrite or URL Rewriting on test.com to detect the example.com hostname and rewrite to itself. This can be dangerous though; better to have example.com have its own vhost on the test.com server, with only redirect rules in it.
    – JasonBirch
    Commented Jul 22, 2010 at 5:55
8

As has been noted, this can not be done entirely with DNS. However, if you have the DNS for example.com refer to the same server as test.com it is very simple to configure Apache to accomplish what you wish to do. Just add the following:

<VirtualHost *:80>
  ServerName example.com
  Redirect permanent / http://test.com/
</VirtualHost>

This will rewrite any incoming link to example.com to a link for test.com, preserving the path.

Furthermore, your DNS provider does not need to host any files for you as this only relies on the web server you are already running for test.com.

1
  • Or just add ServerAlias directives to test.com's virtual host file.. Commented Oct 9, 2013 at 16:31
3

You cannot do this via DNS alone. 301 redirects are not a DNS thing. They're a webserver thing.

What you can do is point your example.com DNS entries to the same server as test.com, and set up example.com as an alias domain to test.com.

3

Here is the exact .htaccess rule that I use to redirect all requests from www.mydomain.com to mydomain.com (since the shorter is my canonical URL):

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

That's easy enough to modify to redirect from foo.com to foobar.com, while preserving the request, the client just gets a 301 redirect to the appropriate domain.

You can't however do this 'just' with DNS, as others have said, DNS servers just resolve FQDN's to IP addresses.

1
  • I used this, but I had to remove the last slash in your second line (RewriteRule ^(.*)$ http://mydomain.com$1 [R=301,L]) or it would add two slashes to the redirected URL. Anyone else experienced that?
    – julien_c
    Commented Jul 19, 2012 at 18:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.