Cấu hình subdomain với DNS, Apache và các sử dụng trong PHP


http://andy.sinaptix.com/2007/08/22/username-as-subdomain-in-codeigniter

http://ma.tt/2003/10/wildcard-dns-and-sub-domains/

I’ve been wondering for a while if I can do automatic subdomain using username, same as WordPress MU does. Some thought come across and all leads to same point , the $base_url() variable.

What makes me a bit wary is that particular variable reside in config file, which suppose to be static. On the other hand, manipulating the variable in controller is easy but redundant. So what the heck…
There are three places to modify for this :

  1. DNS entry
  2. Apache vhost
  3. Codeigniter config.php

Life has been good to me since I have full control over those three, I have my own DNS server, and apache server, so if you are less fortunate than I am, do not hesitate to nag , beg and annoy your hosting company to do changes for you

DNS entry :

make sure you put wildcard entry :
mygreatsite.com. A 123.123.123.001
*.mygreatsite.com. A 123.123.123.001

Apache vhost :

We use separate vhost file for each domain, and include those files using Apache “Include” directive, to keep things tidy and avoid fiddling with main apache config.
<virtualhost>
ServerName mygreatsite.com
ServerAlias *.mygreatsite.com
ServerAdmin superduperadmin@superduperwebcompany.com
#RedirectMatch 301 (.*) http://mygreatsite.com$1
DocumentRoot /var/oi/keepcodeigniterhere
<directory>
Options Indexes FollowSymLinks MultiViews
</directory>
AllowOverride All
Order allow,deny
allow from all
</virtualhost>

the most important part is :
#RedirectMatch 301 (.*) http://mygreatsite.com$1
CodeIgniter config.php :

I test using this simple hack ( on the config.php ) :
$config['base_url'] = “http://”.$_SERVER["HTTP_HOST"];

And wala !! I can test using whatever.mygreatsite.com and codeigniter still working as if there is nothing wrong with $base_url variable, therefore you can use any words ( hence, wildcard ) as subdomain prefix and apache will direct the request to the same codeigniter installation. Next step I will do is using username as subdomain, eliminating ugly url like
http://www.mygreatsite.com/someuser/profile
and make it nicer as in
http://someuser.mygreatsite.com/profile
Nice !!
Yeah I know I have to test rigorously to ensure nothing is broken with codeigniter ( and my application ) but at this moment I think it leads to the right direction.
I also do some search at CodeIgniter’s forum just to verify my method and I’ve found great reference :
http://codeigniter.com/forums/viewthread/51627/

For wildcard DNS, take a look at this guide by WordPress creator :
http://photomatt.net/2003/10/10/wildcard-dns-and-sub-domains/

Gửi phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s