在用户注册时创建子域 [英] Create subdomain upon user registration

查看:28
本文介绍了在用户注册时创建子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,我希望用户在其中注册以获得自己的子域.这个子域是虚拟的,每个子域使用相同的网络服务器文件.

I have a website where I want users that sign up to get their own subdomain. This subdomain is virtual, and every subdomain uses the same web server files.

我使用 PHP 和 Apache,我了解虚拟主机,但我想知道我需要将虚拟主机代码放在哪里.首先,我无权访问 httpd.conf.其次,我希望这在注册时自动完成.

I use PHP and Apache, and I know about Virtual Hosts, but I'm wondering where I need to put the vhosts code. First, I don't have access to httpd.conf. Second, I want this to be done automatically upon registration.

我已经阅读了有关虚拟主机的内容,但没有找到任何可以回答我的问题的内容.有没有人可以向我解释所有这些是如何协同工作的,或者知道我可以在哪里找到答案?

I've read about virtual hosts, but didn't find anything that answers my questions. Is there anyone who can explain me how all this works together, or know where I can find my answers?

推荐答案

你能告诉 apache 读取额外的 .conf 文件吗?(传统上您将虚拟主机存储在 httpd-vhosts.conf 中)

Can you tell apache to read an extra .conf file? (traditionally you store your vhosts in httpd-vhosts.conf)

如果是这样,添加如下内容并重新启动您的网络服务器

if so, add something like the following and restart your webserver

NameVirtualHost *:80

<VirtualHost *:80>
        DocumentRoot /abs/path/to/webroot
        ServerName   domainname.com
        ServerAlias *.domainname.com
        <Directory /abs/path/to/webroot>
                AllowOverride All
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

然后在 php 中,您可以通过检查来查看用户请求的是哪个子域:

then in php, you can see which subdomain the user is requesting by inspecting:

$_SERVER['HTTP_HOST']

即.如果用户请求 http://user1.domainname.com/index.php

$_SERVER['HTTP_HOST'] 将拥有 user1.domainname.com

$_SERVER['HTTP_HOST'] will have user1.domainname.com

然后你可以explode('.', $_SERVER['HTTP_HOST']) 来检查每个段......等等

you can then explode('.', $_SERVER['HTTP_HOST']) to inspect each segment.. etc.

这篇关于在用户注册时创建子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆