Apache2 site redirect on Debian – and basic config ideas

This is a really simple thing, and you only do it once per site, but it’s really easy to do when you know how, and a pain in the ass when you don’t, if you end up being fooled into using modRewrite or similar. A couple of people I spoke to recently hadn’t been doing it this way (which I think is the recommended way?)

0. (yes, there’s a zero, for people who don’t do much Apache configuring) Apache works *really* well if you have ONE separate configuration file per website on your server – and if you have the same site on different ports (e.g. one with SSL, one without), then that counts as two websites. Trust me, it makes life REALLY easy because your config files stay very short, and very easy for you to remember why they say what they say.

1. Make two apache site-configs: one for the real site you want, that will contain all the site config, and one to represent “all the other sites I don’t want”, and just to redirect to the first site
2. Configure the first site to have the public-facing webserver name you want
3. Configure the second site to be a catch-all for all the other variants of your site
4. Add a *permanent* redirect as the sole contents of the second site’s config

Or, with more detailed explanation:

1. Add a second site. If you’re using debian, the lazy way to do this (DON’T DO THIS – see step 2 instead for a pre-written file for you to copy/paste) is to copy /etc/apache2/sites-available/default into a new file in the same directory.
1.b. If you were lazy above, *remember to remove the “NameVirtualHost *” line from the top of your copied file, because that is in the “default” file on the assumption you only ever have one default file; there is no separate “skeleton” file for new sites (although it would be nice if there was).

NB: incidentally, everywhere I’ve worked with debian, we’ve sooner or later written our own skel file for apache site configs. I highly recommend it.

NB2: a “skel file” is just a partly-written config file (“here’s one I made earlier”) that contains the common config options you use for most sites. When you want to create a new site, rather than copying an existing file, and having to remember which bits to remove and change, you instead copy your skel file, which you carefully ensured only had the common parts, and didn’t have anything that would conflict with your existing sites (if two sites on the same server have conflicting config, apache can (and often will) disable both of them – its easy to break an existing site when trying to add a new one :( )

2. In the second site, capture ALL the undesired versions of your URL. This only requires the following config file (so … no need to be super-lazy in part 1 above)

NB: this ASSUMES you only run ONE website on that server that needs redirecting. What it is doing is saying “every HTTP / HTTPS / etc URL that someone uses to connect to this site, and which *no other config file on this server specifically grabs for itself*, belongs to me … use my config file”.

(so, if you need to redirect multiple sites on the same server, you’ll need to have make the config files for the catch-alls more clearly distinguish between each other. Worst case, you’ll need to write out the full list of known aliases in the “ServerAlias *” line, instead of the “*” )

<VirtualHost *>
ServerName [pick any name you don’t like]
ServerAdmin [be polite, include an email address]
ServerAlias *
Redirect permanent / http://[full preferred path goes here]/
</VirtualHost>

3. In the original site config, probably the “default” file if you haven’t made any new sites yet, make sure you name it [full preferred path goes here], i.e. ensure you have the following line at the top somewhere:

ServerName [full preferred path goes here]

That’s it. You’re done.