views:

267

answers:

2

I have domain.com and domain.org as aliases pointing to the same vhost. How can I use .htaccess to redirect all domain.com requests to domain.org?

+2  A: 

You could use mod_rewrite to do this.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.org$
RewriteRule ^ http://example.org%{REQUEST_URI} [L,R=301]

This rule redirects every request that’s not addressed to example.org to the very same.

Gumbo
A: 

Or you could use Redirect permanent like

Redirect permanent / http://domain.org/

which will force the browser to refresh the domain on client side. (While the rewrite would happen server-side.) So the solution depends on what you want.

mattanja
I don’t think this would work. Both host names share the same virtual host so example.org would also be redirected.
Gumbo
Ah ok. So if you can't create two different vhosts the rewrite would be the way to go.
mattanja
The `R` flag used in my example forces an external HTTP redirect with the status code 301.
Gumbo