动态创建子域

发布于 2024-08-20 05:57:47 字数 85 浏览 7 评论 0原文

当用户注册 Blogger 或 WordPress 时,就会获得自己的立即可用的子域。鉴于我有自己的 VPS/VDS/专用服务器,我怎样才能实现同样的目标?

When one signs up for Blogger or WordPress, one gets their very own sub-domain that works instantly. How can I achieve the same, given that I have my own VPS/VDS/Dedicated server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不气馁 2024-08-27 05:57:47

简而言之:

  1. 在 DNS 中创建一个通配符域(即解析whatever.yourdomain.example 返回您的 IP),
  2. 在 Web 服务器中创建默认虚拟主机并
  3. 检查应用程序中的 URL。

如何执行此操作取决于您使用的技术。让我举一些例子:

  1. 如何在 BIND 中设置通配符域 和 < a href="http://technet.microsoft.com/en-us/library/cc288031.aspx" rel="nofollow noreferrer">Windows Server DNS。
  2. 要创建默认虚拟主机,您只需在 IIS 中创建一个没有主机条目的 Web 服务器即可。在 Apache 中,配置文件中列出的第一个虚拟主机成为默认主机。
  3. 在这里,您可以 (a) 根据域重写 URL(即将子域转换为 URL 中的参数,ASP 示例)。 NET,使用 mod_rewrite 的 Apache 示例:Link1Link2),或者 (b) 只需查看 URL 的主机部分(例如ASP.NET 中的 Request.Url)。

bortzmeyer 添加(很抱歉覆盖您的编辑,存在编辑冲突):

通配符的语法,通常DNS 区域文件格式(在 RFC 1035 中描述并在 BIND、nsd 等中实现)带有星号:

 *   IN    A   198.51.100.3

In a nutshell:

  1. Create a wildcard domain in DNS (i.e., resolving whatever.yourdomain.example returns your IP),
  2. create a default virtual host in your web server and
  3. check the URL in your application.

How to do this depends on what technology you use. Let me give you some examples:

  1. How to set up a wildcard domain in BIND and in Windows Server DNS.
  2. To create a default virtual host, you just need to create a web server without a host entry in IIS. In Apache, the first virtual host listed in the configuration file becomes the default host.
  3. Here, you can either (a) rewrite the URL depending on the domain (i.e., converting the subdomain into a parameter in the URL, example for ASP.NET, examples for Apache with mod_rewrite: Link1, Link2), or (b) just have a look at the host part of the URL (e.g. Request.Url in ASP.NET).

Addition by bortzmeyer (sorry for overwriting your edit, there was an edit conflict):

The syntax for a wildcard, in the usual DNS zone file format (described in RFC 1035 and implemented in BIND, nsd and may be others) is with a star:

 *   IN    A   198.51.100.3
℉絮湮 2024-08-27 05:57:47

对于那些对 A 和 CNAME 事物不熟悉的人来说,有一个非常简单的解决方案,并且可以与共享主机配合使用:

只需转到您的 cpanel 并使用 * 添加一个子域,

例如,如果您的域名为 abc.com,您可以添加 * 并选择/输入子目录作为根目录。保存时,它将在您的子域表中添加 *.abc.com,并将所有必要的 A 记录添加到您的区域文件中。

当您在浏览器中点击“any”.abc.com时,服务器会将您带到指定位置(您提到的子目录)。

此外,要处理特定重定向的所有(任何)子域,您可以使用该子目录中的 .htaccess 来处理所有传入的子域请求。

一个有效的 .htaccess 示例如下:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(^.*)\.abc\.com
RewriteRule (.*)  handler.php?user=%1&%{QUERY_STRING}

</IfModule>

handler.php(下面的代码)仅显示一条欢迎消息,其中包含子域名和 URL 中的所有查询字符串:

$user = $_REQUEST["user"];
print_r($_REQUEST);
echo "Welcome {$user}";

希望这会有所帮助。

For those, who are laymen to all this A and CNAME things, there's a very simple solution and works with Shared Hosting:

Simply go to your cpanel and add a subdomain with *

For example, if your domain is called abc.com, you can add * and select/enter the sub-directory as a root to this. When you save, it will add *.abc.com in your sub-domains table and will add all necessary A records to your zonefile.

When you hit "any".abc.com in your browser, server will land you to the specified location (the sub-directory you mentioned).

Additionally, to handle all (any) sub-domain for specific redirection, you can use a .htaccess in that sub-directory to handle all incoming subdomain requests.

A working .htaccess example is as follows:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(^.*)\.abc\.com
RewriteRule (.*)  handler.php?user=%1&%{QUERY_STRING}

</IfModule>

The handler.php (code below) simply displays a welcome message with sub-domain name and all query string in the URL:

$user = $_REQUEST["user"];
print_r($_REQUEST);
echo "Welcome {$user}";

Hope this helps.

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