如何让 PHP 为每个用户自动创建子域?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/183928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to let PHP to create subdomain automatically for each user?
提问by Skuta
How do I create subdomain like http://user.mywebsite.com? Do i have to access htaccess somehow? Is it actually simply possible to create it via pure php code or I need to use some external script-server side language?
如何创建像http://user.mywebsite.com这样的子域?我必须以某种方式访问 htaccess 吗?实际上是否可以通过纯 php 代码创建它,或者我需要使用一些外部脚本服务器端语言?
To those who answered: Well, then, should i ask my hosting if they provide some sort of DNS access??
对于那些回答的人:那么,我应该问我的托管服务商是否提供某种 DNS 访问权限?
采纳答案by Mark Biek
You're looking to create a custom A record.
您正在寻找创建自定义A 记录。
I'm pretty sure that you can use wildcards when specifying A records which would let you do something like this:
我很确定您可以在指定 A 记录时使用通配符,这样您就可以执行以下操作:
*.mywebsite.com IN A 127.0.0.1
127.0.0.1 would be the IP address of your webserver. The method of actually adding the record will depend on your host.
127.0.0.1 将是您的网络服务器的 IP 地址。实际添加记录的方法将取决于您的主机。
Doing it like http://mywebsite.com/userwould be a lot easier to set up if it's an option.
如果它是一个选项,像http://mywebsite.com/user那样做会更容易设置。
Then you could just add a .htaccess file that looks like this:
然后你可以添加一个看起来像这样的 .htaccess 文件:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([aA-zZ])$ dostuff.php?username=
In the above, usernames are limited to the characters a-z
在上面,用户名仅限于字符 az
The rewrite rule for grabbing the subdomain would look like this:
获取子域的重写规则如下所示:
RewriteCond %{HTTP_HOST} ^(^.*)\.mywebsite.com
RewriteRule (.*) dostuff.php?username=%1
回答by balupton
The feature you are after is called Wildcard Subdomains. It allows you not have to setup DNS for each subdomain, and instead use apache rewrites for the redirection. You can find a nice tutorial here, but there are thousands of tutorials out there. Here is the necessary code from that tutorial:
您所追求的功能称为通配符子域。它允许您不必为每个子域设置 DNS,而是使用 apache 重写进行重定向。你可以在这里找到一个很好的教程,但那里有成千上万的教程。这是该教程中的必要代码:
<VirtualHost 111.22.33.55>
DocumentRoot /www/subdomain
ServerName www.domain.tld
ServerAlias *.domain.tld
</VirtualHost>
However as it required the use of VirtualHosts it must be set in the server's httpd.conf file, instead of a local .htaccess.
但是,由于它需要使用 VirtualHosts,因此必须在服务器的 httpd.conf 文件中进行设置,而不是本地 .htaccess。
回答by gradbot
I do it a little different from Mark. I pass the entire domain and grab the subdomain in php.
我做的和马克有点不同。我通过整个域并在 php 中获取子域。
RewriteCond {REQUEST_URI} !\.(png|gif|jpg)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?uri=&hostName=%{HTTP_HOST}
This ignores images and maps everything else to my index.php file. So if I go to
这会忽略图像并将其他所有内容映射到我的 index.php 文件。所以如果我去
http://fred.mywebsite.com/album/Dance/now
I get back
我回来了
http://fred.mywebsite.com/index.php?uri=album/Dance/now&hostName=fred.mywebsite.com
Then in my index.php code i just explode my username off of the hostName. This gives me nice pretty SEO URLs.
然后在我的 index.php 代码中,我只是从主机名中分解出我的用户名。这给了我漂亮漂亮的 SEO URL。
回答by gradbot
We setup wildcard DNS like they explained above. So the a record is *.yourname.com
我们像上面解释的那样设置通配符 DNS。所以 a 记录是 *.yourname.com
Then all of the subdomains are actually going to the same place, but PHP treats each subdomain as a different account.
然后所有的子域实际上都在同一个地方,但 PHP 将每个子域视为不同的帐户。
We use the following code:
我们使用以下代码:
$url=$_SERVER["REQUEST_URI"];
$account=str_replace(".yourdomain.com","",$url);
This code just sets the $accountvariable the same as the subdomain. You could then retrieve their files and other information based on their account.
此代码只是将$account变量设置为与子域相同。然后,您可以根据他们的帐户检索他们的文件和其他信息。
This probably isn't as efficient as the ways they list above, but if you don't have access to BIND and/or limited .htaccess this method should work (as long as your host will setup the wildcard for you).
这可能不如上面列出的方法有效,但如果您无权访问 BIND 和/或有限的 .htaccess,则此方法应该有效(只要您的主机将为您设置通配符)。
We actually use this method to connect to the customers database for a multi-company e-commerce application, but it may work for you as well.
我们实际上使用这种方法连接到多公司电子商务应用程序的客户数据库,但它也可能适用于您。
回答by Willem
Don't fuss around with .htaccess files when you can use Apache mass virtual hosting.
当您可以使用Apache 海量虚拟主机时,不要对 .htaccess 文件大惊小怪。
From the documentation:
从文档:
#include part of the server name in the filenames VirtualDocumentRoot /www/hosts/%2/docs
#include part of the server name in the filenames VirtualDocumentRoot /www/hosts/%2/docs
In a way it's the reverse of your question: every 'subdomain' is a user. If the user does not exist, you get an 404.
在某种程度上,这与您的问题相反:每个“子域”都是用户。如果用户不存在,您会收到 404。
The only drawback is that the environment variable DOCUMENT_ROOTis not correctly set to the usedsubdirectory, but the default document_root in de htconfig.
唯一的缺点是环境变量DOCUMENT_ROOT没有正确设置到使用的子目录,而是在de htconfig中默认的document_root。
回答by Alex Khimich
Simple PHP solution for subdomains and multi-domain web apps
用于子域和多域 Web 应用程序的简单 PHP 解决方案
Step 1. Provide DNS A record as "*" for domains (or domain) you gonna serve "example.org"
第 1 步。为您要提供“example.org”的域(或域)提供 DNS A 记录为“*”
A record => *.example.org
A record => *.example.net
Step 2. Check uniquity of logins when user registering or changing login. Also, avoid dots in those logins.
步骤 2. 用户注册或更改登录时检查登录的唯一性。此外,请避免在这些登录名中使用点。
Step 3. Then check the query
步骤 3. 然后检查查询
// Request was http://qwerty.example.org
$q = explode('.', $_SERVER['HTTP_HOST']);
/*
We get following array
Array
(
[0] => qwerty
[1] => example
[2] => org
)
*/
// Step 4.
// If second piece of array exists, request was for
// SUBDOMAIN which is stored in zero-piece $q[0]
// otherwise it was for DOMAIN
if(isset($q[2])) {
// Find stuff in database for login $q[0] or here it is "qwerty"
// Use $q[1] to check which domain is asked if u serve multiple domains
}
?>
This solution may serve different domains
此解决方案可能服务于不同的域
qwerty.example.org
qwerty.example.net
johnsmith.somecompany.com
paulsmith.somecompany.com
If you need same nicks on different domains served differently, you may need to store user choise for domain when registering login.
如果您需要在不同域上使用不同的相同昵称,您可能需要在注册登录时存储域的用户选择。
smith.example.org // Show info about John Smith
smith.example.net // Show info about Paul Smith
回答by theraccoonbear
In addition to configuration changes on your WWW server to handle the new subdomain, your code would need to be making changes to your DNS records. So, unless you're running your own BIND (or similar), you'll need to figure out how to access your name server provider's configuration. If they don't offer some sort of API, this might get tricky.
除了在您的 WWW 服务器上更改配置以处理新的子域之外,您的代码还需要更改您的 DNS 记录。因此,除非您运行自己的 BIND(或类似的),否则您需要弄清楚如何访问您的名称服务器提供商的配置。如果他们不提供某种 API,这可能会变得棘手。
Update: yes, I would check with your registrar if they're also providing the name server service (as is often the case). I've never explored this option before but I suspect most of the consumer registrars do not. I Googled for GoDaddy APIs and GoDaddy DNS APIs but wasn't able to turn anything up, so I guess the best option would be to check out the online help with your provider, and if that doesn't answer the question, get a hold of their support staff.
更新:是的,我会与您的注册商核实他们是否也提供名称服务器服务(通常是这种情况)。我以前从未探索过这个选项,但我怀疑大多数消费者注册商都没有。我在谷歌上搜索了 GoDaddy API 和 GoDaddy DNS API,但无法找到任何东西,所以我想最好的选择是查看您的提供商的在线帮助,如果这不能回答问题,请稍候他们的支持人员。
回答by warren
You could [potentially] do a rewrite of the URL, but yes: you have to have control of your DNS settings so that when a user is added it gets its own subdomain.
您可以 [潜在地] 重写 URL,但是是的:您必须控制 DNS 设置,以便在添加用户时获得自己的子域。
回答by Dan Bray
First, you need to make sure your server is configured to allow wildcard subdomains. I achieved that in JustHost by creating a subomain manually named *. I also specified a folder called subdomainsas the document root for wildcard subdomains. Add this to a .htaccessfile in your subdomains folder:
首先,您需要确保您的服务器配置为允许通配符子域。我在 JustHost 中通过创建一个手动命名的子域来实现这一点*。我还指定了一个文件夹subdomains作为通配符子域的文档根目录。将此添加到.htaccess您的子域文件夹中的文件中:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):
RewriteRule ^(.*)$ /%1/ [QSA]
Finally, all you need to do is create a folder in your subdomains folder, then place the subdomain's files in that directory.
最后,您需要做的就是在您的子域文件夹中创建一个文件夹,然后将子域的文件放在该目录中。
回答by Dan Bray
This can be achieved in .htaccessprovided 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 your .htaccessfile:
.htaccess如果您的服务器配置为允许通配符子域,则可以实现这一点。我在 JustHost 中通过创建一个手动命名的*子域并指定一个名为 subdomains 的文件夹作为通配符子域的文档根来实现这一点。将此添加到您的.htaccess文件中:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):
RewriteRule ^(.*)$ /%1/ [QSA]
Finally, create a folder for your subdomain and place the subdomains files.
最后,为您的子域创建一个文件夹并放置子域文件。

