【问题标题】:Apache URL rewriting and wildcard subdomain handlingApache URL 重写和通配符子域处理
【发布时间】:2011-12-19 23:57:25
【问题描述】:

我是 PHP 新手,我正在创建一个 Web 应用程序,用户可以在其中注册并获得子域。我已经完成了注册部分,但我想再做一件事,我想不通。 因此,当用户浏览到 example.domain.com 时,如果用户 'example' 已经注册,那么他应该从 domain.com/users/example/ 获取页面,但如果未使用用户名,则应该写“此域尚未注册.blabla”

我怎么能这样做? 在此先感谢,并为我糟糕的英语感到抱歉:)

【问题讨论】:

  • 我不明白你的问题中的标题和非问题之间的关系是什么
  • @Qchmpqs 因为您使用通配符进行 url 重写以便能够向 php 脚本提供信息以验证用户是否存在。

标签: php apache .htaccess mod-rewrite


【解决方案1】:

这可能是您想要的?

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

【讨论】:

  • 感谢您的快速答复。我已经更新了我的 .htaccess 文件,但它给出了一个内部服务器错误。
  • 我稍微改动了上面的,记得把 domain.com 改成你的域名,你还需要添加通配符才能工作。
  • 它仍然抛出错误。我检查了 apache 日志,第一条规则有问题。
  • 又改了,运气好吗?
  • 还是不行。 RewriteRule: cannot compile regular expression '^user/([a-zA-Z9-0-_]+)/$' 这是确切的错误。
【解决方案2】:

首先,创建包含“此域尚未注册。blabla”消息的默认页面。调用这个registration.html

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

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

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

【讨论】:

    猜你喜欢
    • 2012-06-14
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    相关资源
    最近更新 更多