【问题标题】:Custom URLs in Rails of the form custom.site.comRails 中的自定义 URL,格式为 custom.site.com
【发布时间】:2010-09-18 15:06:01
【问题描述】:

我想让我的用户指定自定义 URL 路径,以便将这些路径放在我网站名称的前面,即,如果我有一个名为 www.orion.com 的网站,我希望用户能够在 johnny.orion.com 上创建他自己的小主页。

我已经成功地实现了 orion.com/johnny,它通过在我的路由末尾添加 map.connect ':path' 然后确保在需要时存在路径变量来实现。

如何让 johnny.orion.com 正常运行?

【问题讨论】:

    标签: ruby-on-rails url-routing


    【解决方案1】:

    首先,您需要设置通配符 DNS - 以便子域真正解析到某个地方。

    然后,配置您的虚拟主机以接受来自这些子域的连接:

    # If you're using Apache, something like:
    ServerAlias *.orion.com
    

    然后,您可以使用subdomain-fu gem 在 Rails 中处理您的路线。看看the associated Railscast 以获得一些好的提示。

    gem 的语法是这样的:

    link_to 'Posts', post_path(@post, :subdomain => 'johnny')
    johnny.orion.com/posts/4
    

    【讨论】:

    • 如果你在 Rails 3 上已经有 :subdomain support build in routing。
    猜你喜欢
    • 2014-06-07
    • 2012-09-09
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多