1

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, subdomain.example.com would lead to example.com/subdomain, which is actually example.com/index.php/subdomain.

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.

crash
  • 11
  • 1

1 Answers1

0

Try the following:

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{HTTP_HOST} ^(.*).\crashworks\.co [NC]
RewriteRule ^(.*)$ http://www.crashworks.co/d/%1 [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 ^(.*)$ http://crashworks.co/d/%1 [L,R=301]
farinspace
  • 173
  • 1
  • 1
  • 13
  • 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