Skip to Content

One Rule to rule them all

I own a number of parked domains that all redirect to this site. The redirection is handled by some Rewrite rules in my Apache server's root .htaccess configuration file. For example:

# redirect domain.co.za
RewriteCond %{HTTP_HOST} ^domain\.co.za$ [NC]
RewriteRule ^(.*)$ http://coda.co.za/$1 [R=301,L]

What bugs me is that I have to add these lines for each domain name, and then again for instances when the "www" subdomain prefix is included (I prefer to remove the "www"). Needless to say my .htaccess file is looking mighty bloated these days.

I've hunted around for a more elegant solution with no luck, so here's my question for all the Apache and Regex geeks out there: is there a way to list these domains in a Rewrite condition (as an array of sorts) that will also cover the "www" inclusion/exclusion? So something like this:

RewriteCond %{HTTP_HOST} ^(domain1.co.za|domain2.com)$ [NC]
RewriteRule ^(.*)$ http://coda.co.za/$1 [R=301,L]

The end result I'm hoping for is that "domain1.co.za", "www.domain1.co.za", "domain2.com", "www.domain2.com", etc. will all redirect to "/". Supposing my pipe-delimited array idea isn't possible, what is the most practical way to achieve this?

 

10 Comments

01 July 2008
01:38 pm

Liberta Design

Damian

The regular expression is:

^(www[.]|)(domain1.co.za|domain2.co.za|domain3.co.za)$

Some regular expression interpreters don’t support this type of “extended” regular expressions; I don’t know what engine Apache’s rewrite module uses. Try it and let me know if it works.

Take care,
Francois

01 July 2008
07:20 pm

coda

@Francois: wicked man, thanks for the quick response. The array is working, but Apache doesn’t like the “(www[.]|)” bit. Any ideas?

01 July 2008
08:47 pm

Liberta Design

Hmmm… damn. Not surprised though.

If the problem is with the “[]” for the period, you can just go:

^(www\.|)(domain1.co.za|domain2.co.za|domain3.co.za)$

If the problem is with the first ‘(…)’, this one will match the “www.”, but also “ww.”, “w.”, “ww”, and “w” preceding the domains, so you may run into problems:

^[w]{0,3}[.]{0,1}(domain1.co.za|domain2.co.za|domain3.co.za)$

I suppose you can also try (which suffers from the same problems):

^[w]*[.]*(domain1.co.za|domain2.co.za|domain3.co.za)$

Other than that I suppose you can hack Apache to use a different regex engine. :)

01 July 2008
10:02 pm

coda

This works for me:

^(www.)?(domain1.co.za|domain2.co.za|domain3.co.za)$

01 July 2008
10:09 pm

coda

One step further:

^(www.)?(domain1|domain2|domain3)\.(co.za|com|org)$

02 July 2008
03:18 am

Liberta Design

Beautiful. Thanks Damian, I’m sure this will come in handy some time down the line for me as well.

02 July 2008
03:21 am

Liberta Design

Apologies, I see I’ve been calling you Damian instead of Damien. :)

02 July 2008
06:16 pm

louis w

Couldn’t you do this:

RewriteCond %{HTTP_HOST} !^coda\.co\.za$ [NC]
RewriteRule ^(.*)$ http://coda.co.za/$1 [R=301,L]

(If the host is not the one you want, redirect)

02 July 2008
07:36 pm

coda

@louis: in my case I have a number of domains that I also redirect to specific pages of my site (digicam.co.za and panoramas.co.za for example) so that condition would conflict with those. I suppose it would be simpler to exclude them as an extension of your rule, but I like to see a list of domain names because then I know what I have available.

Leave a Comment

Your e-mail address is required, but will not be published.

 
 
 

Tags allowed:  <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>