Make apache automatically strip off the www.?

后端 未结 6 1931
一生所求
一生所求 2021-01-03 08:56

For various reasons, such as cookies, SEO, and to keep things simple, I would like to make apache automatically redirect any requests for http://www.foobar.com/anything to h

相关标签:
6条回答
  • 2021-01-03 09:10

    Use an .htaccess file with some mod_rewrite rules:

    RewriteEngine On
    RewriteRule ^www.SERVERNAME(.*) http://SERVERNAME$1 [L,QSA]
    

    I'm not sure I got the syntax right with the $1 there, but it's well documented. L sends a location: header to the browser, and QSA means Query String Append.

    0 讨论(0)
  • 2021-01-03 09:10
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
    

    That should do the trick.

    0 讨论(0)
  • 2021-01-03 09:19

    simpler and easier to copy from site to site:

    RewriteCond %{HTTP_HOST} ^www\.(.+)$
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    
    0 讨论(0)
  • 2021-01-03 09:23

    Pretty simple if you use mod_rewrite, as we all do ;)

    This is part of the .htaccess from my live website:

    RewriteEngine on
    
    # Catches www.infinite-labs.net and redirects to the
    # same page on infinite-labs.net to normalize things.
    
    RewriteCond %{HTTP_HOST} ^www\.infinite-labs\.net$
    RewriteRule ^(.*)$ http://infinite-labs.net/$1 [R=301,L]
    
    0 讨论(0)
  • 2021-01-03 09:24

    Since you mentioned using mod_rewrite, I'd suggest a simple rule in your .htaccess - doesn't seem monstrous to me :)

    RewriteCond %{HTTP_HOST} ^www\.foobar\.com$ [NC]
    RewriteRule ^(.*)$ http://foobar.com/$1 [L,R=301]
    
    0 讨论(0)
  • 2021-01-03 09:29

    It's as easy as:

    <VirtualHost 10.0.0.1:80>
            ServerName www.example.com
            Redirect permanent / http://example.com/
    </VirtualHost>
    

    Adapt host names and IPs as needed :)

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