Fun with mod_rewrite and subdomains

Intercepting and redirecting to while still leaving be (for the rare occasion you don't want to visit the pub instead).

Being a fan of the "www. is deprecated" movement, I obviously couldn't be satisfied with the library being accessible from both and, so I went and commented out and edited the lines for redirecting www.(domain) to (domain) in Drupal's default .htaccess file.

However, I also realised that due to the way I've set up the DNS (look up the A record(s)),,,, etc. would all just sit there like did just moments ago. I couldn't just redirect all subdomains either, as the way I have and am doing development of this site (which is an entry in its own right, I'd say), requires me to have four domains - and three subdomains. And as they're all running the same codebase, redirecting all subdomains would lead to my test and development sites breaking. :( After playing around some though, I figured out the key was in first excluding the subdomains I didn't want to redirect, and then throw in everything else (".*"):
RewriteCond %{HTTP_HOST} !^(sub1|sub2|subN)\.freso\.dk$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.freso\.dk$ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

(Which reminds me that I should probably fetch and install some module with a code formatting filter...)

Go ahead and test it. Apart from three subdomains, everything should send you back to the main domain. :)


a little help?

I'm trying to put this into practice and greatly simplify my htaccess file and it's only half working for me, I wonder if you could take a minute to consider what might be wrong.

here's the full contents of my test file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(drupal|test2|test3)\.domain\.org$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.org$ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

What I get is the rewrite works for -> but doesn't rewrite to (nor do the two other subdomains) and will die rather than redirect to

Thanks in advance,

I'd do it the other way

I'd do it the other way around, redirect www to domain, and leave everything else be. Means that is perfectly valid, but if you're doing * aliases anyway...

Or, perhaps slightly more clever: redirect www. to . gets redirected to, getting the point across...

You're missing the point

But. Since I don't want there to be anything on banana.domain (I only want apple.domain for my development/test "mirror"), it'd not make sense to make banana.domain show the exact same things as would be the case if I allowed the domain to be. Right now, if you go to,,, ... you'll get redirected to all the same. What you're suggesting would be a clear regression of this (ie., that would be accessible on its own, mirroring the content of

However, if I suddenly do want to use, I can just set it up on the server (and in the DNS, if the subdomain is to be hosted elsewhere) and throw in files there – as long as it's not using the .htaccess, people won't be thrown away from it (and if we are using the same .htaccess, adding "banana" to the list of exceptions isn't hard). :)

I'd stay away from the *

I'd stay away from the * alias in the first place. If I don't have anything for, there's nothing that quite gets the point across like an missing A record.

That's my point.

If I give to someone because I have somthing to show them there, and they mistype it, they might think that is where they're supposed to be and not check whether they've entered the right address.

Don't do that, and you don't need funky rewriting.

Very nice

And if you don't have any subdomains at the moment, the first line can be commented out, in reserve for when it's ready, and it will all work before and after.

Consolidating additional domains (not subdomains) to one location takes a different tactic though...

Domain redirecting

That's a good point you have about commenting out the line. That is very true indeed. :)

Also, I have yet to have a site running on multiple actual domains, but that could probably be overcome by replacing "freso\.dk" with "(domain1|domain2|domainN)". But then you'd have issues when you don't want all the domains to redirect to the same master domain, or if you only want some subdomains on certain domains... but the above code snippets should provide a base for making the exceptions that should make this possible. I haven't tried it though, so...

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <q> <blockquote> <cite> <abbr> <a> <ins> <del> <strong> <em> <hr> <address> <code> <dfn> <kbd> <pre> <samp> <var> <h2> <h3> <h4> <h5> <h6> <dl> <dt> <dd> <ol> <ul> <li> <caption> <col> <colgroup> <table> <tbody> <td> <tfoot> <th> <thead> <tr> <bdo> <ruby>
  • Lines and paragraphs break automatically.
  • Insert Flickr images: [flickr-photo:id=230452326,size=s] or [flickr-photoset:id=72157594262419167,size=m].
By submitting this form, you accept the Mollom privacy policy.