【问题标题】:Apache: Virtual Host and URL Rewriting inside httpd.conf for Kohana FrameworkApache:Kohana 框架的 httpd.conf 中的虚拟主机和 URL 重写
【发布时间】:2011-05-17 20:55:47
【问题描述】:

我正在使用 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>

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

【问题讨论】:

    标签: apache url-rewriting kohana httpd.conf virtual-hosts


    【解决方案1】:

    如果您在带有REQUEST_FILENAME 条件的VirtualHost 块中使用mod_rewrite,您将不会 获得文件名(这是根据the documentation)。相反,此变量包含与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 有一天返回实际文件名,以避免文档根目录的双前缀。

    【讨论】:

    • 救命!谢谢!
    【解决方案2】:

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

    【讨论】:

    • 当我使用带有 .htaccess 文件的每个目录的 URL 重写时,我认为我只需要一个 RewriteBase。如果我的 RewriteBase 像“/”,我不能事件启动 apache 并得到一个错误。我不想再使用 .htaccess 文件,因为我想将所有设置集中在我的 httpd.conf 中
    • .htaccess 有很多问题,主要是每次请求时,请求路径中文档根目录下的每个目录都需要额外的磁盘访问。如果您关闭扫描此类文件 (AllowOverride None),您的服务器将运行得更快。
    【解决方案3】:

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

    检查一下,可能会有所帮助。

    【讨论】:

      【解决方案4】:

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

      RewriteRule ^(?:aplication|modules|system)\b.* index.php/$0 [L]
      

      【讨论】:

        猜你喜欢
        • 2012-05-16
        • 1970-01-01
        • 2012-05-26
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-20
        • 2018-07-04
        相关资源
        最近更新 更多