Remove WWW prefix from your website

前端 未结 9 682
情歌与酒
情歌与酒 2020-12-28 19:24

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?<

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-28 20:12

    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.

提交回复
热议问题