Rob
Rob

Reputation: 1708

Remove WWW prefix from your website

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

Upvotes: 15

Views: 12080

Answers (8)

MC.
MC.

Reputation: 481

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

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.cuenca.co$ [NC]
RewriteRule ^(.*)$ http://cuenca.co/$1 [R=301,L]

Upvotes: 5

L Stevens
L Stevens

Reputation: 1

For apache

<VirtualHost *:80>
ServerName yourdomain.tld
ServerAlias www.yourdomain.tld

Upvotes: 0

Cheeso
Cheeso

Reputation: 192457

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.

Upvotes: 0

Chad Grant
Chad Grant

Reputation: 45382

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.

Upvotes: 1

Gideon
Gideon

Reputation: 18491

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.

Upvotes: 10

Greg Hewgill
Greg Hewgill

Reputation: 992877

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.

Upvotes: 12

Steven Richards
Steven Richards

Reputation: 2822

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]

Upvotes: 13

Matt Kocaj
Matt Kocaj

Reputation: 11535

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

Upvotes: 4

Related Questions