动态子域 PHP

发布于 2024-09-28 03:05:37 字数 431 浏览 6 评论 0原文

好吧,这个问题以前可能已经得到了答案,但显然到目前为止我仍然没有找到答案。

您可能会注意到,有些网站可能允许用户注册以立即在网站中获得自己的子域。

例如,域名是 www.domain.com。 如果我注册一个新用户为 henson,我将在网站上获得自己的页面,例如:www.henson.domain.com(不确定 www 部分是否必要) 因此,如果用户打开 www.henson.domain.com,它实际上会打开 www.domain.com?owner=henson

我可以仅使用 htaccess 来执行此操作吗?因为我在某处读到这还需要在 cpanel 中手动创建子域(这违背了网站的目的)。

哦,该网站是用平面 PHP 编码的,所以没有 MVC 框架。如果有人知道如何使用框架(最好是 CodeIgniter)轻松做到这一点,欢迎回答。

感谢您的回答。

Okay, this might have been answered before but apparently up until now I still haven't found the answer.

You may notice there are some websites that may allow users to register to instantly get a subdomain of their own in the website.

For example, the domain is www.domain.com.
If I register a new user as henson, I will get my own page in the website, ex: www.henson.domain.com (not sure if the www part is necessary)
So if a user open www.henson.domain.com, it will actually open www.domain.com?owner=henson

Can I do this using only htaccess? Because I read somewhere that this also needs manual creation of subdomains in cpanel (which defeats the purpose of the website).

Oh, the website is coded with flat PHP, so no MVC frameworks. IF someone knows how to do this easily with frameworks (preferably CodeIgniter), be welcome to answer.

Thanks for the answer.

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

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

发布评论

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

评论(4

故人的歌 2024-10-05 03:05:37

你不能在htaccess中,你必须设置一个通配符虚拟主机并将其重写到你需要的URL/目录。请参阅http://blog.orite.com.au/web_development/2009-01-22/setting-up-wildcard-virtual-hosts-for-web-development-environment/ 了解更多信息

You cant in htaccess, you must setup a wild card virtual host and rewrite it to the URL/directory you require. See http://blog.orite.com.au/web_development/2009-01-22/setting-up-wildcard-virtual-hosts-for-web-development-environment/ for more info

忆悲凉 2024-10-05 03:05:37

如果您的服务器上有 CPANEL,则有一个 XMLAPI 允许您通过 PHP 动态创建子域。

是的,动态的,而不是手动的。我刚刚花了过去 2 天的时间来处理这个问题(动态创建从子域到电子邮件帐户到附加域和 sql 数据库、用户......一切的所有内容),而 cpanel API 干净利落地处理了这一切。所以花点时间弄清楚吧。

在此页面的第一个链接下载 XMLAPI:
http://forums.cpanel .net/f42/xml-api-php-class-version-1-0-a-136449.html。文件 xmlapi.php 是您的服务器上唯一需要的文件。

该论坛页面是一个噩梦般的坟墓,由非常高级和/或非常黑客的程序员编写的半个工作示例组成,没有明确的起点。

这是 PHP 中的一个基本脚本,用于添加子域,用您的个人值替换大写字母。这花了我很长时间才搞清楚。祝你好运!下一步,点击该论坛链接并阅读所有其他 API1 和 API2 函数!

include("PATH_TO_THE_DOWNLOADED_xmlapi.php");

    $ip = "YOUR_IP_ADDRESS";
$root_pass = "ROOT_CPANEL_PASSWORD!";


$xmlapi = new xmlapi($ip);
$xmlapi->password_auth("root",$root_pass);

$account = "YOUR_CPANEL_MAIN_ACCNT_NAME";


print $xmlapi->api2_query($account, 'SubDomain','addsubdomain', array(dir=>"public_html/NAME_OF_SUBDOMAIN", domain=>"NAME_OF_SUBDOMAIN", rootdomain=>"MAIN_DOMAIN.com") );

If you have CPANEL on your server, there is an XMLAPI which allows you to dynamically creates subdomains through PHP.

Yes, dynamically, not manually. I just spent the last 2 days on this (dynamic creation of everything from subdomains to email accnts to addon domains and sql dbs, users....everything), and cpanel API handles it all, cleanly. So take a while and figure it out.

Download the XMLAPI at the first link on this page:
http://forums.cpanel.net/f42/xml-api-php-class-version-1-0-a-136449.html. The file xmlapi.php is the only one you need on your server.

That forum page is a nightmarish graveyard of half working examples written by very advanced and/or very hacky coders with no clear starting point.

Here is a basic script in PHP to add subdomains, replace the caps w your personal values. This took me quite a while to get right. Best of luck! Next steps, hit that forum link and read all the other API1 and API2 functions!

include("PATH_TO_THE_DOWNLOADED_xmlapi.php");

    $ip = "YOUR_IP_ADDRESS";
$root_pass = "ROOT_CPANEL_PASSWORD!";


$xmlapi = new xmlapi($ip);
$xmlapi->password_auth("root",$root_pass);

$account = "YOUR_CPANEL_MAIN_ACCNT_NAME";


print $xmlapi->api2_query($account, 'SubDomain','addsubdomain', array(dir=>"public_html/NAME_OF_SUBDOMAIN", domain=>"NAME_OF_SUBDOMAIN", rootdomain=>"MAIN_DOMAIN.com") );
×纯※雪 2024-10-05 03:05:37

您可以解析 $_SERVER['SERVER_NAME'] 来确定使用哪个子域。 www 部分实际上是不必要的。这只是一个没有任何意义的子域。大多数时候它只是简单地映射到主域。例子:

if (preg_match('/^(www\.)?(.+)\.your-domain.com$/', $_SERVER['SERVER_NAME'], $matches) && $matches[2] != 'www') {
    $subdomain = $matches[2];
    // your logic goes here
}

You can parse $_SERVER['SERVER_NAME'] to determine which subdomain is used. The www part is actually never necessary. This is simply a subdomain that has no meaning. Most of the time it is simply mapped to the main domain. Example:

if (preg_match('/^(www\.)?(.+)\.your-domain.com$/', $_SERVER['SERVER_NAME'], $matches) && $matches[2] != 'www') {
    $subdomain = $matches[2];
    // your logic goes here
}
几味少女 2024-10-05 03:05:37

如果您的服务器配置为允许通配符子域,则可以在 .htaccess 中实现此目的。我在 JustHost 中通过手动创建一个名为 * 的子域并指定一个名为 subdomains 的文件夹作为通配符子域的文档根来实现这一点。将其添加到子域文件夹中的 .htaccess 文件中:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]

最后,为子域创建一个文件夹并放置子域文件。

This can be achieved in .htaccess provided your server is configured to allow wildcard subdomains. I achieved that in JustHost by creating a subomain manually named * and specifying a folder called subdomains as the document root for wildcard subdomains. Add this to a .htaccess file in your subdomains folder:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]

Finally, create a folder for your subdomain and place the subdomains files.

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