【问题标题】:Simple URL Rewrite with wildcard subdomain使用通配符子域的简单 URL 重写
【发布时间】:2012-11-08 10:01:16
【问题描述】:

我希望每个用户都有唯一的子域地址,例如 http://*.mywebsite.com,其中 * 可以是任何用户名,例如 xyz、abc、john、deo 等。

我希望每当有人打开像http://aswt.mywebsite.com 这样的网址时,它会被重定向到http://www.mywebsite.com/panels/users/index.php?subdomain=aswt

请帮助我实现它,因为我是 url 重写的新手。我确实尝试过在谷歌上搜索和堆栈溢出,但没有找到与我相似或易于使用的东西。

【问题讨论】:

    标签: php mod-rewrite url-rewriting


    【解决方案1】:

    AFAIK,你不能用 mod_rewrite 做到这一点:RewriteRule 不考虑 HTTP_HOST。 RewriteCond 可以,但在这里还不够。

    你必须用 PHP 来做:

    if (preg_match('#^(.*)\.mywebsite\.com$#', $_SERVER['HTTP_HOST'], $matches))
    {
      header('Location: /panels/users/index.php?subdomain='.$matches[1];
      exit;
    }
    

    应该可以...

    无论如何,对于 url 重写(尤其是复杂的),PHP 导致的问题通常比 mod_rewrite 少得多,并且功能更强大。

    【讨论】:

    猜你喜欢
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2012-08-09
    • 2014-06-09
    • 1970-01-01
    相关资源
    最近更新 更多