Remove WWW prefix from your website

前端 未结 9 597
情歌与酒
情歌与酒 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:02

    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.

    0 讨论(0)
  • 2020-12-28 20:03

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

    0 讨论(0)
  • 2020-12-28 20:11

    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]
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-28 20:16

    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.

    0 讨论(0)
  • 2020-12-28 20:16

    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.

    0 讨论(0)
提交回复
热议问题