#123 Subdomains

Learn how to unleash the full potential of subdomains with the subdomain-fu Rails plugin in this episode.

Domain Name Solutions

As mentioned in the episode, you’ll need to add each subdomain to /etc/hosts. This can be a problem if subdomains can be generated dynamically. It would be nice if we could add a catch-all domain. Here are a couple solutions.

One is to make a PAC file and set it up as the proxy in your web browser preferences. It might look something like this.


# proxy.pac
function FindProxyForURL(url, host) {
if (shExpMatch(url,"*.local/*")) {
return "PROXY localhost";
}
return "DIRECT";
}


Another solution is to set up your own domain name server on your localhost and create an A entry to catch all subdomains. Sorry I can’t provide detailed instructions for this. Use Google. :)

Source Code from Episode
script/plugin install git://github.com/mbleigh/subdomain-fu.git
sudo apachectl graceful
dscacheutil -flushcache

# /etc/hosts
127.0.0.1 personal.blog.local company.blog.local

# apache config
ServerAlias *.blog.local# routes.rb
map.blog_root '', :controller => 'blogs', :action => 'show', :conditions => { :subdomain => /.+/ }
map.root :blogs

# initializers/subdomain_config.rb
SubdomainFu.tld_sizes = {:development => 1,
:test => 0,
:production => 1}

# controllers/application.rb
def load_blog
@current_blog = Blog.find_by_subdomain(current_subdomain)
if @current_blog.nil?
flash[:error] = "Blog invalid"
redirect_to root_url
end
end

# articles_controller.rb
before_filter :load_blog

def index
@articles = @current_blog.articles.find(:all)
end

def show
@article = @current_blog.articles.find(params[:id])
@comment = Comment.new(:article => @article)
end

def new
@article = @current_blog.articles.build
end

<!-- blogs/index.html.erb -->
<%= link_to h(blog.name), blog_root_url(:subdomain => blog.subdomain) %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值