如何让PHP为每个用户自动创建子域?

问题描述:

如何创建类似于 http://user.mywebsite.com 的子域?我必须以某种方式访问​​htaccess吗?是否真的可以通过纯PHP代码简单地创建它,或者我需要使用一些外部脚本服务器端语言?

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?

对那些回答的人:那么,我是否应该问我的托管服务商他们是否提供某种类型的DNS访问?

To those who answered: Well, then, should i ask my hosting if they provide some sort of DNS access??

您要创建自定义的 A记录.

我很确定指定A记录时可以使用通配符,这样您就可以执行以下操作:

I'm pretty sure that you can use wildcards when specifying A records which would let you do something like this:

*.mywebsite.com       IN  A       127.0.0.1

127.0.0.1将是您的Web服务器的IP地址.实际添加记录的方法取决于您的主机.

http://mywebsite.com/user 这样操作,如果可以的话,设置起来会容易得多.一个选择.

Doing it like http://mywebsite.com/user would be a lot easier to set up if it's an option.

然后,您可以添加一个如下所示的.htaccess文件:

Then you could just add a .htaccess file that looks like this:

Options +FollowSymLinks

RewriteEngine On
RewriteRule ^([aA-zZ])$  dostuff.php?username=$1

在上面,用户名仅限于字符a-z

获取子域的重写规则如下:

The rewrite rule for grabbing the subdomain would look like this:

RewriteCond %{HTTP_HOST} ^(^.*)\.mywebsite.com
RewriteRule (.*)  dostuff.php?username=%1