2012-03-16 132 views
2

尝试使用重写规则解决此问题,例如,该子规则将子域分配给同名的根目录。重写链接规则,冲突的文件和文件夹

ddd.example.com会链接到“/ _projects/ddd”目录,工作正常,我没有问题,问题是我在根目录“/”中的任何文件或目录都可以从子域ddd.example.com访问。

下面是一个例子的目录结构

example.com = “/”

  • “的index.php”

ddd.example.com = “/ _projects/ddd”

  • 没有文件

所以,如果比如我访问ddd.example.com/index.php,这将解决使用文件位于example.com/index.php这是在下面找到一个目录。

这里是的.htaccess

# Skip rewrite if subdomain is www 
RewriteCond %{HTTP_HOST} !^www\. [NC] 

# Extract (required) subdomain to %1 
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com(:80)?$ 

# Redirect to domain if requested URL does not resolve to existing subdirectory path 
RewriteCond %{DOCUMENT_ROOT}/_projects/%1 !-d 
RewriteRule (.*) http://example.com/ [NC,R=301] 

# Skip rewrite if subdomain is www 
RewriteCond %{HTTP_HOST} !^www\. [NC] 

# Extract (required) subdomain to %1 
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com(:80)?$ 

# Skip rewrite if requested URL does not resolve to existing subdirectory path or file 
RewriteCond %{DOCUMENT_ROOT}/_projects/%1/$1 -f [OR] 
RewriteCond %{DOCUMENT_ROOT}/_projects/%1/$1 -d 
RewriteRule (.*) /_projects/%1/$1 [NC,L] 

回答

0

解决了这个问题,似乎有是被打破了重写一个循环的问题。

##### Subdomain to subfolder 
# Fix missing trailing slashes. 
#RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC] 
#RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC] 
#RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d 
#RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L] 

# Rewrite sub domains. 
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC] 
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC] 
RewriteRule ^(.*)$ /projects/%2/$1 [QSA,L] 
0

如果什么重写规则你RewriteConds失败?然后,URL通过并且不被重写。所以它访问文档根目录。我将为每个受支持的子域创建单独的VirtualHost条目。 (有多少人?)

假设客户要求http://sub.example.com/index.php

假设存在/_projects/sub/index.php

您的RewriteCond -s会看到/_projects/sub/index.php作为文件存在,然后跳过重写。但是,如果重写被跳过,那么没有重定向到/_projects/sub/。那么在这种情况下提取的文件是什么?你猜对了,/index.php

您应该无条件地将这些子域名重定向到适当的位置(仅针对循环检查)。

为什么你将重写分成两部分,一部分做内部重定向?内部重定向不会将整个URL重写为example.com,因此它保留在子域中。看起来你可以在那里进入一个循环。

+0

回复是位于下方。 – Upperfoot 2012-03-17 14:04:34

0

我重写的尝试基本上是这样做的。

伪代码:

if (subdomain-directory != exists) 
    redirect them to the home page 
else 
    rewrite the request for the subdomain 

我只能做到使用两个规则,我还没有发现任何其他的方式来做到这一点,所以这是我的尝试。

有问题的条件实际上正常工作,如果我在/ _projects/sub目录中有一个index.php,那么它将使用该文件,并将其放入其中的任何其他文件相同。

我完全不知道如何用mod_rewrite来完成这个任务,我已经在几个星期的最佳时间内对它进行了无用处理,无尽地寻找可能的解决方案并且没有取得任何进展。