【发布时间】:2011-08-17 09:55:39
【问题描述】:
问题:
我正在使用 Kohana/PHP 为其他公司开发托管网站。 我让客户在他们的 DNS 服务器中输入一个 CNAME 条目以指向我的域。例如。 http://invites.somecompany.com 指向http://www.mydomain.com。
因此,我的 apache 服务器上的 %{HTTP_HOST} 条目是 'invites.somecompany.com'
我想把http://invites.somecompany.com/invite改写成http://www.mydomain.com/invites/invite
尽管 Apache 似乎正在这样做,但 $_SERVER['REQUEST_URI'] 仍然是“/”。 问题是 Kohana 使用 $_SERVER['REQUEST_URI'] 将请求路由到适当的控制器代码。在这种情况下,它将它路由到 base 索引控制器,而不是 'invites' 控制器。
事实:
我正在使用的 Apache mod_rewrite 指令(在 .htaccess 文件中):-
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteCond %{REQUEST_URI} !.*invites.*
RewriteRule ^(.*)$ invites/$1
# For Kohana
RewriteRule .* index.php?kohana_uri=$0 [PT,QSA,L]
在 index.php 中,我这样做了:
var_dump($_SERVER);
我明白了:
'REQUEST_URI' => string '/',
'QUERY_STRING' => string 'kohana_uri=invites/index.php&kohana_uri=invites/invite'
'REDIRECT_QUERY_STRING' => string 'kohana_uri=invites/invite'
所以 mod_rewrite 不会修改 REQUEST_URI ?
需要:
'REQUEST_URI' => 'invites/invite',
'QUERY_STRING' => string 'kohana_uri=invites/invite',
我如何得到它?
======================= 编辑
重写日志条目:-
strip per-dir prefix: /Users/project/invite -> invite
applying pattern '^(?:application|modules|system)\b.*' to uri 'invite'
strip per-dir prefix: /Users/project/invite -> invite
applying pattern '\.git' to uri 'invite'
strip per-dir prefix: /Users/project/invite -> invite
applying pattern '^(.*)$' to uri 'invite'
rewrite invite -> invites/invite
add per-dir prefix: invites/invite -> /Users/project/invites/invite
strip per-dir prefix: /Users/project/invites/invite -> invites/invite
applying pattern '.*' to uri 'invites/invite'
rewrite invites/invite -> index.php?kohana_uri=invites/invite
add per-dir prefix: index.php -> /Users/project/index.php
strip document_root prefix: /Users/project/index.php -> /index.php
internal redirect with /index.php [INTERNAL REDIRECT]
strip per-dir prefix: /Users/project/index.php -> index.php
applying pattern '^(?:application|modules|system)\b.*' to uri 'index.php'
strip per-dir prefix: /Users/project/index.php -> index.php
applying pattern '\.git' to uri 'index.php'
strip per-dir prefix: /Users/project/index.php -> index.php
applying pattern '^(.*)$' to uri 'index.php'
rewrite index.php -> invites/index.php
add per-dir prefix: invites/index.php -> /Users/project/invites/index.php
strip per-dir prefix: /Users/project/invites/index.php -> invites/index.php
applying pattern '.*' to uri 'invites/index.php'
rewrite invites/index.php -> index.php?kohana_uri=invites/index.php
add per-dir prefix: index.php -> /Users/project/index.php
initial URL equal rewritten URL: /Users/project/index.php [IGNORING REWRITE]
【问题讨论】:
-
这就是 mod_rewrite 的工作原理。
标签: php apache mod-rewrite dns kohana