ovgolovin
ovgolovin

Reputation: 13410

Understanding why plain redirect is used

Recently I took class on Startup Engineering.

While working on a topic of DNS system I found out that sites tend to use redirects to point their different domain names to the main one.

For example, Google use redirect to www.google.com when google.com is accessed:

> curl -I google.com

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
...

From what I know, redirects slow things down, as to get info where to redirect to, it takes the signal to go double the distance to the web-server, so that increasing overall latency.

I have been told that browsers cache the info of redirects, so that the next google.com query will be redirected at once by the browser. But the first one still has to get response about redirect by the server.

It seems to me we can achieve the same thing at DNS level. I know we can't have CNAME record for google.com leading to www.google.com (as it would turn all email [email protected] into [email protected]). But we can use redirect at DNS level. Or DNS ALIAS. As the last step we can change url in the address bar of the browser from google.com to www.google.com with use of JavaScript. As to me, this will lead to the same result as using redirect, but will avoid us of having redirect and so that we will serve the resultant page faster.

Still, there should be a reason why plain redirect is used.

So, what is this reason? What is the advantage of using redirect? What are the caveats of redirecting at DNS level?

Upvotes: 0

Views: 148

Answers (2)

ovgolovin
ovgolovin

Reputation: 13410

It seems that JavaScript can't change address in the browser address bar beyond same origin.

So the only way to change google.com to www.google.com is to redirect.

All the redirects and ALIASes at DNS level happen on the side of user DNS server, which returns final IP address of web-server to the user. The hostname sent by browser via HTTP to the server is at much higher level of OSI model. And still web-server receives the original hostname, the only way to change it in the browser address bar is to redirect to the new url.

Upvotes: 0

Carl Levine
Carl Levine

Reputation: 1

Redirects are used as RFC 1912 does not permit the use of CNAME records on the apex of a DNS zone. Several DNS providers have built things like "DNS ALIAS" to use server-side redirects on their DNS edge machines to resolve the queries, the 301/302 redirect is simply a way of shunting HTTP traffic to another FQDN. Latency stemming from the use of a 301/302 redirect may vary from one DNS provider to another.

Upvotes: 1

Related Questions