子域mod_rewrite(htaccess)到相对目录

时间:2013-02-10 22:59:30

标签: .htaccess mod-rewrite subdomain relative-path

我在将子域重写到位于以上 Web根目录的目录时遇到了一些麻烦。我之前有过很多关于mod_rewrite的经验,但这个特殊问题超出了我的范围。

据我所知,mod_rewrite正在发脾气,因为我坚持使用相对目录(..)来确定子域文件所在的目录。

不幸的是,我的客户规格有两个限制:

  • 将子域作为Web根目录的子目录不是一个选项。除了特定的子域(可能存在目录冲突)之外,不能从任何地方访问子域。 这意味着http://subdomain.example.com/必须 才能从http://example.com/subdomain/访问,因为该目录可能会在根域中的应用程序中使用。
  • 客户端不知道子域文件的绝对路径,因为将使用共享主机。

如果有人能帮我解决这个问题,我将不胜感激!我很乐意在未来的项目中开始使用它,与我们目前处理子域的方式相比,这是一个非常优雅的解决方案......如果有人能够让它工作就好了!

修改:您可能有必要指出,在请求http://subdomain.example.com/时,会返回400 Bad Request,而不是我预期的500 Internal Server Error。请求根域时,一切都按预期工作。

当前.htaccess个文件。

# Begin Rewrite Module for http://*.example.com/
# ==============================================
<IfModule mod_rewrite.c>

    # Turn the rewrite engine on.
    RewriteEngine On
    RewriteBase /

    # Map subdomains to their respective directories.
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
    RewriteRule (.*) /../public_subdomains/%1/$1 [L]

    # Rewrite all requests for the root domain to always be served through "index.php".
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) /index.php/$1 [L]

</IfModule>

当前目录结构。

/
    application/
    cgi-bin/
    framework/
    public_html
        public/
            css/
            images/
            js/
        .htaccess
        index.php
    public_subdomains/
        mysubdomain/
            index.php
        anothersubdomain/
            index.php

1 个答案:

答案 0 :(得分:1)

如何实现此操作取决于主机如何实现子域。有些只是映射到您的DOCROOT。其他提供了一个控制面板,允许您执行该子域 - &gt;子域docroot自己。如果(2)适用,那么你已经提供了你想要的东西,所以我将在这个ans中假设(1)。

首先要注意的是,在htaccess per-directory上下文中重写实际上是URI映射到了DOCROOT层次结构上。 “..”是不允许的,并且会抛出500.在实践中,你被困在域名的DOCROOT中。

因此,public_domains 必须是DOCROOT的子目录 - 在您的情况下public_html

但是,您仍然可以通过简单的规则阻止对public_html/public_domains的任何直接访问:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI}         /public_domains(/|$)
RewriteRule ^                      -                 [F,L]

有关更多提示,请参阅我的Tips for debugging .htaccess rewrite rules。您只想在入口通行证上使用public_domains对请求进行barf。还要记住,对于/.

的基础,不包括前导/和目标相对于DOCROOT
相关问题