Easy Apache Redirects and Site Aliases

Everybody knows about using .htaccess files to redirect, say, foo.com to www.foo.com. There’s lots of cookbooks out there for doing this. But I was in a different situation; I registered foo.com, foo.biz, foo.net, foo.org, foo.mobi, foo.us, and foo.info!

(Note: “Everybody” refers to apache sysadmins, and foo refers to a domain name that I do not want to advertise at this time.)

One way to handle this would be to set up a ServerAlias entry in the httpd.conf file for every one of these domains. While this works, it doesn’t redirect the domain. Instead I end up with duplicate content for every one of these many domains.

I could handle this by writing a .htaccess file that covers every combination of, say, foo.net, foo.org, foo.biz, and so forth for every domain, but that seemed like an awful lot of typing. Yet, for some reason, I couldn’t find much of anything on the web to make this easier.

It turns out this can all be handled in your httpd.conf file using the “Redirect” directive.

What you need to do is set up two virtual hosts. One is for the domain you’re re-directing to, and the other is for any and all domains you want to redirect from. Then you add a “Redirect permanent” directive as appropriate.

Here’s an example, in which I try to route foo.com to www.foo.com:

# This is the master domain
<VirtualHost *:80>
ServerName www.foo.com
DocumentRoot /var/www/foo.com
</VirtualHost>

# Redirect foo.com to www.foo.com
<VirtualHost *:80>
ServerName foo.com
Redirect permanent / http://www.foo.com
</VirtualHost>

Okay, this is actually more text that an .htaccess mod_rewrite statement, but look at what happens when I add ServerAliases to the redirecting virtual server:

# Redirect everything to www.foo.com
<VirtualHost *:80>
ServerName foo.com
ServerAlias *.foo.com
ServerAlias foo.net *.foo.net
ServerAlias foo.org *.foo.org
ServerAlias foo.biz *.foo.biz
[and so on...]
Redirect permanent / http://www.foo.com
</VirtualHost>

I can use the same syntax to do a 302, 303, 401 or any other sort of redirect as well.

If you have a fairly simple array of virtual hosts, you could also use the default “catch all” virtual host to redirect to your main site, thus avoiding any need to explicitly define tons of ServerAlias entries.

sohbet

What you need to do is set up

What you need to do is set up two virtual hosts. One is for the domain you’re re-directing to, and the other is for any and all domains you want to redirect from. Then you add a “Redirect permanent” directive as appropriate.

Although Drupal offers a

Although Drupal offers a sophisticated programming interface for developers, no programming skills are required for basic website installation and administration.

The Apache Redirect directive

The Apache Redirect directive allows you to tell clients and browsers about documents which used to exist in one place but have since moved.

Thanks for the interesting

Thanks for the interesting insights and great tips. I have not thought of guest posting in the way you mentioned, to a wider audience, but will think of it in this way now. Thank you!

holiday to portugal

Apache web server

Apache web server configuration to enable a web site redirect. covers use of the .htaccess. The YoLinux portal covers topics from desktop to servers!

cheap Vienna hotels

Post new comment

The content of this field is kept private and will not be shown publicly.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.