且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-24 11:21:50

这可能是你想要的?

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