【问题标题】:RewriteRule for handling multiple domains within one single app?RewriteRule 用于在一个应用程序中处理多个域?
【发布时间】:2013-03-21 17:06:17
【问题描述】:

假设我有一个应用example.com.

一个客户可能在example.com/customer/hisname/拥有它的内容。

好吧,我想让客户为他的内容分配一个域,比如hisname.com,所以这个(hisdomain.com)呈现@987654324 @。

为此,我创建了两个虚拟主机,它们的根目录都在同一个位置,并且我编写了一个 .htaccess 文件以过滤 http_host,如果不是 example.com,则以这样的方式重写它将请求传递给 example.com/customer/hisname/。

尽管如此,当我没有得到 URI 中的 /customer/hisname/ 部分时,这种方法仍然不起作用。 如果我访问 example.com/index.php 中的 hisname.com/list/,我需要同时拥有 /list/ 和(作为前缀)/customer/hisname/,我只会得到,如您所料只是 /list/。

那么,什么是正确的方法呢?

【问题讨论】:

    标签: php .htaccess mod-rewrite url-rewriting virtualhost


    【解决方案1】:

    如果你可以创建Virtual Hosts,你应该设置正确的文档根目录而不是使用mod_rewrite

    <VirtualHost w.x.y.z:80>
        ServerAdmin webmaster@hisname.com
        DocumentRoot /path/to/www/customer/hisname
        ServerName hisname.com
        ...
    </VirtualHost>
    

    如果这不可行,您可以提取host 部分并进行内部重写

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{HTTP_HOST} !^(?www\.)?example\.com$
    RewriteCond %{HTTP_HOST} ^(.+)\.com$
    RewriteRule ^ /index.php?prefix=customer/%1&request=%{REQUEST_URI} [L,QSA]
    

    【讨论】:

    • 我刚刚再次尝试了您建议的第一种方法。所以,我有一个看起来像这样的 .htaccess: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f [OR] RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*) index.php 和 2 个虚拟主机:一个有 DocumentRoot ” D:/XAMPP Servers/xampp/htdocs/domains_app/customer/" 另一个有 DocumentRoot "D:/XAMPP Servers/xampp/htdocs/domains_app/" 虽然第二个工作正常,但第二个带来 500 错误(请求超出10 个内部重定向的限制...)。
    • @Michael RewriteCond %{REQUEST_FILENAME} !-f [OR] RewriteCond %{REQUEST_FILENAME} !-d 总是正确的,因为OR。请查看更新的答案。
    • 我想让第一种方法工作,同时我可以创建虚拟主机,但我已经删除了那个逻辑运算符,我仍然得到 500 错误。我不知道这是从哪里来的..
    • 问题似乎出在 DocumentRoot 上。如果我将其设置为 [..]domains_app/customer/ 它会触发 500 错误。如果我删除 /customer/ 它可以工作。现在,当然,客户文件夹不存在 - 它将由调度程序解释,最后控制器将进入阶段..
    • 如果我为两个虚拟主机设置了相同的文档根目录,那么 htaccess 就可以完成这项工作。对于不暗示使用 RewriteRule 的方法,您是否有任何其他提示/参考?我可能做错了..谢谢!
    【解决方案2】:

    使用变量 $_SERVER['HTTP_HOST'] 并​​根据请求的主机加载不同的视图。确保您处理所有需要的子主机并拥有一个默认页面

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      相关资源
      最近更新 更多