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
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.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
That should do the trick.
simpler and easier to copy from site to site:
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
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]
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]
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 :)