如何使用 PHP 创建子域?可以在共享主机上吗?
我有兴趣直接从 php 动态创建子域。例如,当用户创建新页面时,我希望该页面为 newpage.mydomain.com。是否可以在不更改 php 或 apache 配置文件的情况下实现(假设我使用共享托管帐户)。
稍后编辑:我正在谈论我的域,并且我拥有对域管理区域的完全访问权限。
I'm interested to create subdomains on the fly directly from php. For example when an user create a new page I want that page to be newpage.mydomain.com. Is that possible without changing the php or apache configuration files (supposing I'm using a shared hosting account).
Later Edit: I'm talking about my domain, and I have full access to the domain administration area.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 .htaccess 文件中设置一个错误文档,将每个 404 重定向到名为“redirection.php”的文件。这个 .php 文件将处理“即时”重定向。
在您的区域文件中添加通配符 DNS 记录,以便 [whateverhere].yourdomain.com 指向您的网络服务器的 IP
使用以下命令在您的 apache 配置中添加通配符服务器别名:
ServerAlias *.yourdomain.com
在您的redirection.php 文件中写入以下代码。
。
这有一点帮助吗?
Set up an error document in your .htaccess file that redirects every single 404 to a file called maybe redirection.php. This .php file is what will handle the "on the fly" redirects.
Add a wildcard DNS record in your zone files, so that [whateverhere].yourdomain.com points to the IP of your webserver
Add a wildcard serveralias in your apache configs by using:
ServerAlias *.yourdomain.com
Write the following code in your redirection.php file.
.
Does this help a bit?
是的,你可以这样做。
您需要确保首先在域名注册 DN 面板中针对您的域名设置通配符 * A 记录。
一旦你设置了通配符,你就可以查看>
Yes you can do this.
You need to make sure that first you set up a wildcard * A record against your domain in your domain register DN panel.
Once you have set up the wildcard, you can just look at the >
仅通过 php 是不可能的。我认为共享主机不允许这样做。
无论如何,因为你必须拥有一个域(或有权编辑 DNS 记录),那么你可以添加通配符记录以允许任何子域指向单个计算机(由其 IP 地址标识)
编辑(来自 Powerlord 评论)
apache 有将每个子域重定向到同一个虚拟主机
通常在虚拟主机配置
/编辑
中使用
ServerAlias *.example.com
然后在 php 中,您可以通过解析(分割)$_SERVER['HOST_NAME']<来检查从子域请求页面的哪个页面/代码>
例如:
it's not possible through php alone. and I don't think shared hosting allow that.
anyway, for that you have to own a domain (or have permission to edit DNS record) then you can add wildcard record to allow any subdomain to point to a single machine (identified by its IP adress)
edit (from Powerlord comment)
apache have to redirect each subdomain to the same vhost
usually with
ServerAlias *.example.com
in a vhost configuration/edit
then in php you can check which from subdomain the page is request by parsing(spliting)
$_SERVER['HOST_NAME']
eg: