如何在 web 应用程序中创建子域用户帐户
我希望允许用户控制我正在使用的应用程序的子域,就像 Basecamp 一样,它是 customusername.seework.com
。
DNS
端需要什么才能动态创建这些内容并立即可用。
您建议如何在网站逻辑中处理这个问题? Htaccess
规则在 DB
中查找子域?
I am looking to allow users to control of subdomain of an app I am toying with, much like Basecamp where it is customusername.seework.com
.
What is required on the DNS
end to allow these to be created dynamically and be available instantly.
And how do you recommend dealing with this in the logic of the site? Htaccess
rule to lookup the subdomain in the DB
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实现这一点的技巧是使用 URL 重写,以便 name.domain.com 透明地映射到服务器上的 domain.com/users/name 等内容。 一旦你开始沿着这条路走下去,实施起来就相当简单了。
The trick to that is to use URL rewriting so that name.domain.com transparently maps to something like domain.com/users/name on your server. Once you start down that path, it's fairly trivial to implement.
我想做类似的事情(
www.mysite.com/SomeUser
)。我所做的是编辑
404.shtml
以包含此服务器端包含 (SSI) 代码:然后我创建了文件
404.php
,在其中解析了 URL 以进行检查获取用户名并显示数据库中的信息。I was looking to do something similar (
www.mysite.com/SomeUser
).What I did was I edited
404.shtml
to include this server side include (SSI) code:Then I created the file
404.php
, where I parsed the URL to check for a user's name and showed their info from the database.不必担心 DNS 和 URL 重写
您的 DNS 记录将是静态的,例如:
请您的 DNS 提供商为您执行此操作(如果尚未执行),或者如果您有,请自行执行此操作控制您的 DNS 记录。 这将自动将您的所有子域(当前和未来的子域)指向同一个 HTTP 服务器。
完成后,您只需解析每个 http 请求上的 HOST 标头,即可检测用于访问 http 服务器上的服务器端脚本的主机名。
假设您使用的是 ASP.NET,这是我提出的一个愚蠢的示例,但它可以工作并演示这种方法的简单性:
Don't worry about DNS and URL rewriting
Your DNS record will be static, something like:
Ask your DNS provider to do it for you (if it's not done already) or do it by yourself if you have control over your DNS records. This will automatically point all your subdomains (current and future ones) into the same HTTP server.
Once it's done, you will only need to parse HOST header on every single http request to detect what hostname was used to access your server-side scripts on your http server.
Assuming you're using ASP.NET, this is kind of silly example I came up with but works and demonstrates simplicity of this approach:
我们这样做的方法是对我们在 DNS 中注册的域名进行“包罗万象”,以便 everything.ourdomain.com 将指向我们的服务器。
使用 Apache,您可以为您的虚拟主机设置类似的包罗万象。 ServerName 必须是单个静态名称,但 ServerAlias 指令可以包含模式。
现在所有域都将触发我们项目的虚拟主机。 最后一部分是解码实际使用的域名,以便您可以在代码中计算出用户名,例如 (PHP):
或已经建议的 RewriteRule,以静默方式将 user.ourdomain.com/foo/bar 映射到 www。 ourdomain.com/foo/bar?user=user 或您喜欢的任何内容。
The way we do this is to have a 'catch all' for our domain name registered in DNS so that anything.ourdomain.com will point to our server.
With Apache you can set up a similar catch-all for your vhosts. The ServerName must be a single static name but the ServerAlias directive can contain a pattern.
Now all of the domains will trigger the vhost for our project. The final part is to decode the domain name actually used so that you can work out the username in your code, something like (PHP):
or a RewriteRule as already suggested that silently maps user.ourdomain.com/foo/bar to www.ourdomain.com/foo/bar?user=user or whatever you prefer.