Sign up ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

For example, my domain is and when someone navigates to, I want it to redirect to

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

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

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

That's easy enough to modify to redirect from to, 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 ^(.*)$$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

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

<VirtualHost *:80>
  Redirect permanent /

This will rewrite any incoming link to to a link for, 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

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

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 DNS entries to the same server as, and set up as an alias domain to

share|improve this answer

You could make 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 ( 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
Yeah, if you set up the CNAME on, you don't have to host anything for Just set the host to respond for, then use mod_rewrite or URL Rewriting on to detect the hostname and rewrite to itself. This can be dangerous though; better to have have its own vhost on the server, with only redirect rules in it. – JasonBirch Jul 22 '10 at 5:55

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.