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?
- leave a comment
10 Comments
@Francois: wicked man, thanks for the quick response. The array is working, but Apache doesn’t like the “(www[.]|)” bit. Any ideas?
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. :)
Beautiful. Thanks Damian, I’m sure this will come in handy some time down the line for me as well.
Apologies, I see I’ve been calling you Damian instead of Damien. :)
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)
@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.
Thank you very myuch
Leave a Comment
Your e-mail address is required, but will not be published.
01 July 2008
Liberta Design01:38 pm
Damian
The regular expression is:
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