如何在 web 应用程序中创建子域用户帐户

发布于 2024-07-04 00:26:22 字数 217 浏览 10 评论 0原文

我希望允许用户控制我正在使用的应用程序的子域,就像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淑女气质 2024-07-11 00:26:22

实现这一点的技巧是使用 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.

半岛未凉 2024-07-11 00:26:22

我想做类似的事情(www.mysite.com/SomeUser)。

我所做的是编辑 404.shtml 以包含此服务器端包含 (SSI) 代码:

<!--#include virtual="404.php" -- >

然后我创建了文件 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:

<!--#include virtual="404.php" -- >

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.

风苍溪 2024-07-11 00:26:22

不必担心 DNS 和 URL 重写

您的 DNS 记录将是静态的,例如:

*.YOURDOMAIN.COM A 123.123.123.123

请您的 DNS 提供商为您执行此操作(如果尚未执行),或者如果您有,请自行执行此操作控制您的 DNS 记录。 这将自动将您的所有子域(当前和未来的子域)指向同一个 HTTP 服务器。

完成后,您只需解析每个 http 请求上的 HOST 标头,即可检测用于访问 http 服务器上的服务器端脚本的主机名。

假设您使用的是 ASP.NET,这是我提出的一个愚蠢的示例,但它可以工作并演示这种方法的简单性:

<%@ Language="C#" %>
<%
string subDomain = Request.Url.Host.Split('.')[0].ToUpper();
if (subDomain == "CLIENTXXX") Response.Write("Hello CLIENTXXX, your secret number is 33");
else if (subDomain == "CLIENTYYY") Response.Write("Hello CLIENTYYY, your secret number is 44");
else Response.Write(subDomain+" doesn't exist");
%>

Don't worry about DNS and URL rewriting

Your DNS record will be static, something like:

*.YOURDOMAIN.COM A 123.123.123.123

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:

<%@ Language="C#" %>
<%
string subDomain = Request.Url.Host.Split('.')[0].ToUpper();
if (subDomain == "CLIENTXXX") Response.Write("Hello CLIENTXXX, your secret number is 33");
else if (subDomain == "CLIENTYYY") Response.Write("Hello CLIENTYYY, your secret number is 44");
else Response.Write(subDomain+" doesn't exist");
%>
云朵有点甜 2024-07-11 00:26:22

我们这样做的方法是对我们在 DNS 中注册的域名进行“包罗万象”,以便 everything.ourdomain.com 将指向我们的服务器。

使用 Apache,您可以为您的虚拟主机设置类似的包罗万象。 ServerName 必须是单个静态名称,但 ServerAlias 指令可以包含模式。

Servername www.ourdomain.com
ServerAlias *.ourdomain.com

现在所有域都将触发我们项目的虚拟主机。 最后一部分是解码实际使用的域名,以便您可以在代码中计算出用户名,例如 (PHP):

list( $username ) = explode( ".", $_SERVER[ "HTTP_HOST" ] );

或已经建议的 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.

Servername www.ourdomain.com
ServerAlias *.ourdomain.com

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):

list( $username ) = explode( ".", $_SERVER[ "HTTP_HOST" ] );

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文