Apache URL重写和通配符子域处理


Apache URL rewriting and wildcard subdomain handling

我是一个PHP新手,我正在创建一个web应用程序,用户可以注册,并可以得到一个子域。我已经完成了注册部分,但我还想再做一件我想不明白的事。因此,当用户浏览example.domain.com时,如果用户'example'已经注册,那么他应该从domain.com/users/example/获得页面,但如果用户名没有被占用,它应该写"此域尚未注册。鼓励性"

我怎么能这么做?提前感谢,很抱歉我的英语很差

这可能是你想要的?

RewriteEngine On
RewriteBase /   
RewriteCond %{HTTP_HOST} !^www'.domain'.com$ [NC]
RewriteCond %{HTTP_HOST} !^domain'.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^'.]+)'.domain'.com$ [NC]
RewriteRule ^$ /users/%1/ [L]
RewriteRule ^user/([a-zA-Z0-9-_]+)/$ user.php?username=$1 [L]

然后你可以在php文件中管理显示部分,如果没有用户关联,则重定向。

您还必须在域名的httpd.conf文件中添加通配符注释,以便它支持通配符子域:

ServerAlias domain.com www.domain.com
ServerAlias *.domain.com

User.php应该是:

<?
if(mysql_num_rows(mysql_query("SELECT id FROM db WHERE username = 'mysql_real_escape_string($_GET[username])'") > 0)
{
    echo "User Exists";
}
else
{
    header("Location: /register.php?username=$_GET[username]");
}
?>

首先,创建包含消息"此域尚未注册"的默认页面。鼓励性产业"。调用registration.html

我假设你有一个存储注册域名的数据库。

搜索数据库查看example.domain.com是否已注册

if(example.domain.com registered)
http_redirect (domain.com/users/example/);
else  
http_redirect (registration.html);