Remove WWW prefix from your website

2019-02-02 12:02发布

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

标签: url web
9条回答
forever°为你锁心
2楼-- · 2019-02-02 12:18

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.

查看更多
beautiful°
3楼-- · 2019-02-02 12:20

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.

查看更多
【Aperson】
4楼-- · 2019-02-02 12:20

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.

查看更多
老娘就宠你
5楼-- · 2019-02-02 12:23

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.

查看更多
劳资没心,怎么记你
6楼-- · 2019-02-02 12:24

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]
查看更多
欢心
7楼-- · 2019-02-02 12:32

For apache

<VirtualHost *:80>
ServerName yourdomain.tld
ServerAlias www.yourdomain.tld
查看更多
登录 后发表回答