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条回答
趁早两清
2楼-- · 2019-02-02 12:34

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]
查看更多
ゆ 、 Hurt°
3楼-- · 2019-02-02 12:39

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.

查看更多
forever°为你锁心
4楼-- · 2019-02-02 12:40

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

查看更多
登录 后发表回答