0

我正在使用 Kohana Framework 3.x。我的网络服务器是 Apache,我使用虚拟主机,因为我用我的服务器管理多个网站。

我的 httpd.conf 看起来像这样:

  <VirtualHost *:80>
 ServerName www.myPage1.com
 ServerAlias myPage1.com
 DocumentRoot /var/www/myPage1
</VirtualHost>
<VirtualHost *:80>
 ServerName www.myPage2.com
 ServerAlias myPage2.de
 DocumentRoot /var/www/myPage2
</VirtualHost>

在 Kohana 中,每个 http 请求都需要先转到 index.php。因为我不喜欢这些以 index.php 开头的丑陋 URL(例如 www.myPage1.com/index.php/item/detail/itemId),所以我使用了以下完美运行的 .htaccess 文件

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All
</Files>

# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

我现在不想再使用 .htaccess 文件,而是将所有重写逻辑放入我的 httpd.conf 文件中。以下给了我一个“400 Bad Request”

<VirtualHost *:80>
 RewriteEngine On
 <Files .*>
  Order Deny,Allow
  Deny From All
 </Files>
 RewriteRule ^(?:aplication|modules|system)\b.* index.php/$0 [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule .* index.php/$0 [PT]

 ServerName www.myPage2.com
 ServerAlias myPage2.com
 DocumentRoot /var/www/myPage2
</VirtualHost>

我究竟做错了什么?帮助将不胜感激!

4

4 回答 4

3

如果您在VirtualHost具有条件的块中使用 mod_rewrite REQUEST_FILENAME,您将不会获得文件名(这是根据文档)。相反,此变量包含与 相同的值REQUEST_URI,其中包含不包括查询字符串的路径。

从上面链接的页面引用:

如果在每个服务器上下文中使用(即,在请求映射到文件系统之前) SCRIPT_FILENAME 和 REQUEST_FILENAME 不能包含完整的本地文件系统路径,因为在这个处理阶段路径是未知的。在这种情况下,这两个变量最初都将包含 REQUEST_URI 的值。为了在每个服务器上下文中获取请求的完整本地文件系统路径,请使用基于 URL 的前瞻 %{LA-U:REQUEST_FILENAME} 来确定 REQUEST_FILENAME 的最终值。

但是,我发现建议的解决方案%{LA-U:REQUEST_FILENAME} 不起作用至少不适用于我服务器上的虚拟主机。为了确保您的RewriteCond行在VirtualHost块中按预期运行,您应该在它们前面加上DOCUMENT_ROOT,因此:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f

此方法将始终有效。切换到_URI以防万一REQUEST_FILENAME有一天返回实际文件名,以避免文档根目录的双前缀。

于 2013-07-25T07:18:19.637 回答
2

您的 httpd.conf 中没有 RewriteBase。.htaccess 有什么问题?

于 2010-11-30T15:24:18.707 回答
1

请记住,如果您将这些规则放在主服务器 conf 文件(通常是 httpd.conf)而不是 .htaccess 文件中,您将需要使用 ^/... ... 而不是 ^... ... at RewriteRule 行的开头,换句话说,添加一个斜线。

检查这个,可能会有所帮助。

于 2011-10-20T03:21:17.887 回答
1

会不会是这个错字(也许应该是“应用程序”):

RewriteRule ^(?:aplication|modules|system)\b.* index.php/$0 [L]
于 2011-03-08T19:56:55.263 回答