Question

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 http://foobar.com/anything. The best I could come up with is a mod_rewrite-based monstrosity, is there some easy simple way to tell it "Redirect all requests for domain ABC to XYZ"?

PS: I found this somewhat related question, but it's for IIS and does the opposite of what I want. Also it's still complex.

Was it helpful?

Solution

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 :)

OTHER TIPS

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]

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.

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]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

That should do the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top