Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

On my new site I want to have dynamic subdomains. I'm trying to make it so that the subdomains use the same web root as the main domain, all under a single CodeIgniter installation. For example, would lead to, which is actually

I've already the DNS, virtual hosts set up but I"m getting caught up on the .htaccess.

The effect of the linked htaccess is that when navigating to any subdomain, it gets caught up in an infinite loop. (Error log after one request.) It's the same effect for www., which should just resolve to the main domain.

share|improve this question

1 Answer 1

Try the following:

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{HTTP_HOST} ^(.*).\crashworks\.co [NC]
RewriteRule ^(.*)$ [L,R=301]

The important things to notice here is the use of (.*) in the RewriteCond and the %1 which is the backreference targeting the RewriteCond matches. Additionally the !^www prevents the infinite loop.

See the apache docs for additional documentation on mod_rewrite.

I've also included a /d/ as it may be easier to do your CodeIgniter routing with it, that is, having a specific controller and/or route setup.

Side Note: Also, consider using www with your site/domain setup, I've come to realize that it becomes much easier to write rules and generally other admin/server related config when dealing with multiple hosts of a single domain.

Unlike the above rules, I haven't tested the following rules which should work without the www (and you can keep the "enforce no www" rules):

RewriteCond %{HTTP_HOST} !^crashworks\.co [NC]
RewriteCond %{HTTP_HOST} ^(.*).\crashworks\.co [NC]
RewriteRule ^(.*)$ [L,R=301]
share|improve this answer
How will this interact with the existing rules? – crash Mar 11 '11 at 11:41
it shouldn't have any negative interactions with existing rules except for the "Enforce NO www" rules, the rules above do depend on the main domain using a "www" ... see the second set of rules which should work with no "www" – farinspace Mar 12 '11 at 21:27
Doesn't seem to be working.. Is it supposed to be an external redirect? I'm going for the transparent look. – crash Mar 15 '11 at 22:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.