子域重写逻辑

时间:2010-07-27 03:07:56

标签: apache mod-rewrite subdomain

我有以下重写逻辑,它正在努力重定向子域,部分重定向。如果我访问网址http://subdomain.clientcollabhq.com/some/path,它会正确重定向,但如果我访问http://subdomain.clientcollabhq.com/,它会跳过子域的RewriteCond块并进入后备RewriteRule。知道发生了什么事吗?

#
# ClientCollabHQ
#
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.localhost
    ServerName clientcollabhq.com
    ServerAlias *.clientcollabhq.com

    DirectoryIndex index.php
    DocumentRoot "D:/wamp/www/dropbox/My Dropbox/ClientCollabHQ/www/html"
    ErrorLog "D:/wamp/www/dropbox/My Dropbox/ClientCollabHQ/www/logs/error.log"

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf)$ [OR]
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -l
    RewriteRule ^ - [L]

    RewriteCond %{HTTP_HOST} !^www\.clientcollabhq\.com$ [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.clientcollabhq\.com$ [NC]
    RewriteRule ^(.*)$ /index.php?subdomain=%2&kohana_uri=$1 [PT,L,QSA]

    RewriteRule ^(.*)$ /index.php?kohana_uri=$1 [PT,L,QSA]
</VirtualHost>

的问候,
安德鲁

1 个答案:

答案 0 :(得分:1)

我不确定“回退”是指哪个部分,但对于网址http://subdomain.clientcollabhq.com/,以下条件之一是真的(我很确定它是-d,因为我认为DirectoryIndex不应该被应用了:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]

原因是您的子域指向您的站点根目录,这恰好是一个真正的资源。由于条件为真,因此不会进行重写,并且最终应该在index.php处没有参数。您可以通过向第一个集添加另一个条件来解决此问题,这将确保在短路后面的规则之前请求除root之外的其他内容。

例如:

RewriteCond %{REQUEST_URI} ^/.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]