Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question
    
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 Jul 21 '10 at 23:37
    
can anyone give a clear and simple reply about this....:( –  user6472 Mar 31 '11 at 13:56
    
@fatty, what don't you understand? –  John Conde Mar 31 '11 at 14:06

4 Answers 4

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.

share|improve this answer
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 Jul 22 '10 at 5:55

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.

share|improve this answer

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.

share|improve this answer
    
Or just add ServerAlias directives to test.com's virtual host file.. –  Michael Clerx Oct 9 '13 at 16:31

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.

share|improve this answer
    
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 Jul 19 '12 at 18:53

protected by John Conde Apr 17 '13 at 11:41

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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