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

2023-09-02 20:34:33 作者:伤了、才明白坚强

我是一个PHP新手,我创建一个web应用程序,用户可以注册并可以得到一个子域。我已经做了登记的一部分,但我想做的一件事,我想不通。 因此,当用户浏览到example.domain.com如果用户'如'已经注册那么他应该从domain.com/users/example/得到的页面,但如果不采取用户名,应该写上该域名尚未注册。布拉布拉

I'm a PHP newbie and I'm creating a web app where users can register and can get a subdomain. I've already done the registration part, but I want to do one more thing which I can't figure out. So when a user browses to example.domain.com if user 'example' already registered then he should get the page from domain.com/users/example/ but if the username is not taken it should write "This domain is not registered yet. blabla"

我怎么能这样做? 在此先感谢,和对不起我的英文不好:)

How could I do this? thanks in advance, and sorry for my poor english :)

推荐答案

这可能是你想要的?

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文件中管理显示部分,如果没有关联,然后用户重定向。

Then you could manage the display part within the php file and if no user associated then redirect.

您还需要一个通配符注释添加到域,这样是httpd.conf文件支持通配符子域:

You also have to add a wildcard comment to the httpd.conf file for the domain so that is supports wildcard subdomains:

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

user.php的会是这样的:

User.php would be something like:

<?
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]");
}
?>