tags:

views:

1196

answers:

8

How does Stack Overflow (and other web sites) remove the 'www' prefix when it's entered as part of a URL?

Is it a redirect, a rewrite or something else entirely?

Update: I'd specifically like to know in the context of IIS 6

+4  A: 

redirect. the sub-domain "www.stackoverflow.com" would simply redirect to "stackoverflow.com".

cottsak
How is the redirect actioned: code, dns or server?
Rob
given that we are talking about SO here, a MVC app, running on IIS, most likely a programmatic redirect i'd say. next DNS. Apache scripts dont apply if it's IIS your interested in.
cottsak
+5  A: 

On Apache, it looks like this (inside an .htaccess file):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Steven Richards
+3  A: 

You can do it several ways, using mod_rewrite and redirecting is my favorite. Something like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mauriciocuenca.com$ [NC]
RewriteRule ^(.*)$ http://mauriciocuenca.com/$1 [R=301,L]
MC
+5  A: 

An easy way to do this is using the Apache "Redirect" directive:

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

<VirtualHost *:80>
    ServerName example.com
    # remainder of server configuration goes here
</VirtualHost>

The Redirect directive automatically preserves anything following the / in the URL. I find this method easier to read and understand than the Rewrite method.

Greg Hewgill
A: 

This is going a long way back, but as far as I know this is a DNS setup. I think you don't need to specify a HOST address (WWW is the name of the host (or computer/cluster...) that the site resides on/in.).

I think you can then set it up to send all requests to a default host.

Not 100% sure, but check out what is possible with DNS.

Hope that helps or at least get's you going in the right direction.

Gineer
+8  A: 

Firing up Fiddler, we can see that the server responses with a "301 Moved Permanently" status and refers it to http://stackoverflow.com . Since StackOverflow is hosted on Windows 2k8 IIS7 they set up this redirect straight away in IIS7.

FYI:

a list of HTTP statuses

If you are a .NET developer you might know "Respose.Redirect" , this creates a 302 Object Moved status. Search engines like 301 status codes in this case better, because they know they should not come back to www.stackoverflow.com in the future.

Gidon
+1  A: 

You need a default dns entry added pointing to your web server.

ping site.com and verify ip is pointing to webserver, if not you need to get the default DNS entry added.

for a basic setup:

You'll have to add host headers http://www.visualwin.com/host-header/

Create 1 site with a hostheader of www.site.com

In the Home Directory tab, set it to a permanent redirect to http://site.com

Create a 2nd site with a host header of site.com

If you want www.site.com/file.html to redirect to site.com/file.html you will need a more advanced setup with something like ISAPI_Rewrite or use custom 404 pages to do it.

Chad Grant
A: 

You can do what mod_rewrite does for Apache, with a comparable URL rewriter for IIS. A good one is IIRF. The rule is:

RewriteCond  %{HTTP_HOST}  ^www\.example\.com$     [I]
RedirectRule ^(.*)$        http://example.com/$1   [R=301]

You can also wildcard the hostname like so:

RewriteCond  %{HTTP_HOST}  ^(.+)\.example\.com$    [I]
RedirectRule ^(.*)$        http://example.com/$1   [R=301]

IIRF is free to use.

Cheeso