Nik's Blog

Geekery, witty insights, software (of dubious quality) and more!

apache

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

ServerName www.foo.com
DocumentRoot /var/www/foo.com


# Redirect foo.com to www.foo.com

ServerName foo.com
Redirect permanent / http://www.foo.com

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

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

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.

Subscribe to RSS - apache

@inik

inik: I love Dropbox because it's indispensible on an iPhone. Was the top of the list of things I recommended to my bro. http://t.co/AnMAp1K4 >
inik: RT @alexismadrigal: Yup, @Salon cut the number of stories they post, increased their quality, and got a 40% traffic boost. http://t.co/X ... >
inik: RT @mikememoli: "The NFL: We're working on that whole brain injury thing." >
inik: I am a sucker for dog ads. Add Ponda Baba and it's like bliss. >
inik: Live stream only available to Verizon customers. Wonder how much that cost them. Anyone know? #superbowl >

Google+

I love Seth's quote at the top. I think that's my new motto.. ; )

Powered by Plu.sr
>
Griping about OS X Lion? Here's two nifty tools that replace a variety of poorly supported third party tools: Command-line and Automator access to video and audio conversion. Super easy to use, and very flexible and supports any format that Quicktime can encode/decode. (So Perian is a must-install if you want to handle DivX/3viX, etc.)

Yes, ffmpeg, Handbrake and...
>
Fix Google Reader's horrible new interface with this user script! Now it fits nicely on my MacBook's small screen. >
Happy 11/11/11 11:11:11! >
What makes this ad awesome is not the true-to-life irony, because the idea is hardly innovative, but rather the excellent execution. Reminds me a bit of that excellent Nutri-Grain spec commercial. Quick delivery, good actors, hit all the high notes. Love it. >