Connecting Tech Pros Worldwide Forums | Help | Site Map

remapping dynamic subdomain

gregerly's Avatar
Expert
 
Join Date: Sep 2006
Posts: 189
#1: May 27 '09
Hi, I'm working on a web app and want to be able to offer my users a URL such as:

username.mydomain.com

but have it map to:

mydomain.com/controller/method/username

FYI, I'm using codeigniter PHP framework, which should explain the above URL I need to map to.

I'm thinking this should be done with mod_rewrite, and I've tried various method to no avail. Any ideas on how to go about this?

Thanks for any insights!

Greg

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#2: May 30 '09

re: remapping dynamic subdomain


I'm afraid you'll likely need access to the server's setup. No idea how you'd go about it, though.
gregerly's Avatar
Expert
 
Join Date: Sep 2006
Posts: 189
#3: May 30 '09

re: remapping dynamic subdomain


Hey Markus,

I have a vps, so I can do what ever. I'm just not sure exactly how to set it up. I added a wildcard domain, so all subdomains resolve, I just need the .htaccess to pick up the slack.

I actually had something working OK, but it redirected all subdomains. There are some I do not want redirected, like the main application, the demo version, the dev version, etc...all which are on their own subdomain.

So I need like a list of domains not to remap, and anything else should get kicked to a specific url.

Thanks for your reply!

Greg
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#4: May 30 '09

re: remapping dynamic subdomain


You can use the RewriteCond stuff for that. Something like this (untested):

Expand|Select|Wrap|Line Numbers
  1. RewriteCond %{HTTP_HOST} !^sub1.site.com
  2.  
I will do some testing to find the correct syntax.
gregerly's Avatar
Expert
 
Join Date: Sep 2006
Posts: 189
#5: May 30 '09

re: remapping dynamic subdomain


Hey no problem Markus, I appreciate your input. Ok, so it's fairly simple it looks like, I think the hardest part is figuring out how to phrase the google query to find what I'm looking for. I should have guessed it would be ! (not) this domain.

I'll see what I can work out with this. Thanks again!

Greg
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#6: May 30 '09

re: remapping dynamic subdomain


I think my above syntax is correct. For multiple sites, you could do this:

Expand|Select|Wrap|Line Numbers
  1. RewriteCond %{HTTP_HOST} !^sub1.site.com
  2. RewriteCond %{HTTP_HOST} !^sub2.site.com
  3. RewriteCond %{HTTP_HOST} !^sub3.site.com
  4. # etc..
  5. # And then your rewrite rule.
  6.  
n8kindt's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Southern California
Posts: 221
#7: Jun 21 '09

re: remapping dynamic subdomain


i just spent all day trying to figure this one out for myself. i started off not even knowing how to use regex to feeling like a guru on this one. i pieced this information from the internet and it has nothing to do with codeigniter but it works for me. like i said i'm still a beginner so any critique of this code is welcome

Expand|Select|Wrap|Line Numbers
  1. #quietly redirect subdomain user profile requests
  2. RewriteCond %{ENV:REDIRECT_STATUS} ^$
  3. RewriteCond %{REQUEST_URI} !(/.+)$
  4. RewriteCond %{HTTP_HOST} ^([a-z-]+)\_([a-z-]+)/*\.thedomain\.com$ [NC]
  5. RewriteRule ^(.*)$ /profile/?first=%1&last=%2 [QSA,L]
  6.  
  7. #ensure links go to the right place so css, js, etc work
  8. RewriteCond %{HTTP_HOST} !^([a-z-]+)\_([a-z-]+)/*\.thedomain\.com [NC]
  9. #also, if not a subdomain add www. for ssl cert (not completely necessary)
  10. RewriteCond %{HTTP_HOST} !^www\.thedomain\.com [NC]
  11. RewriteRule ^(.*)$ http://www.thedomain.com/$1 [R=301,L]
so http://ann-marie_jones.thedomain.com
displays http://www.thedomain.com/profile/?first=ann-marie&last=jones

as long as a wildcard subdomain is set up, this code should be good to go
Reply

Tags
.htaccess, mod_rewrite