【发布时间】:2013-01-26 01:13:49
【问题描述】:
我在将子域重写到位于 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
【问题讨论】:
标签: .htaccess mod-rewrite subdomain relative-path