DNS redirect domain.example to www.domain.example [closed]
Asked Answered
B

1

83

I have performed a web search for the question but totally disappointed with the results. The case is to redirect all requests to domain.example to subdomain www.domain.example.

So what I have:

  1. www.domain.example - main website domain, all client requests should be redirected here
  2. domain.example - another website entry point for people not using www prefix, all requests should be redirected to www.domain.example
  3. mydomain.example - alternative website alias, all requests should be redirected to www.domain.example
  4. www.mydomain.example - for people using www prefix, all requests should be redirected to www.domain.example

I understand this is possible using .htaccess and PHP. But I want to figure out how this can be done using DNS only. I also understand that the result of the DNS query doesn't change what happens in the HTTP layer so the originally entered domain name will always be the one that's sent to the web server in the Host. So to rewrite domain.example to www.domain.example I will still need Apache mod_rewrite. But I want to do main part of work with DNS (CNAME and A records).

So the main question is what CNAME and A records each domain above should have?

Bolitho answered 2/3, 2013 at 8:22 Comment(3)
In response to those who closed the question: wouldn't it be more productive to link this question to a relevant one on the "appropriate" site (presumably serverfault)? And that DNS is completely "off topic" for a web developer is somewhat weak an argument. I really think there's too much eagerness to shut questions down. This was, by the way, was the top google link. It reflects poorly on the community to have this overzealous "off topic" policing of top search results.Recce
Here I am, a web developer, wondering how I redirect calls without the www subdomain, to that.Jitters
I second Emmel, I had this coming first in a search result and this is definitely a web developer question.Conant
C
83

You could make www.example.com the A record and all the other domainnames CNAMEs of www.example.com. But this only "solves" that if the IP address of www.example.com changes you don't have to alter the other DNS enties as they are aliases.

So on the DNS level there is no way to enforce a redirect. And for a good reason because DNS is used for more then only HTTP. For example if all request for example.com would redirect to www.example.com your email addresses will change to [email protected].

So for HTTP redirection you will have to use an HTTP solution. This can be at the webserver level (mod_rewrite, in code, JavaScript (ugh), etc..) but you could also have a proxy in front of your webserver to handle this.

Chlorinate answered 2/3, 2013 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.