Apache:Kohana 框架的 httpd.conf 内的虚拟主机和 URL 重写

发布于 2024-10-05 08:21:00 字数 1636 浏览 6 评论 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。因为我不喜欢这些丑陋的 URL,全部以 index.php 开头(例如 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>

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

I'm using the Kohana Framework 3.x. My Webserver is Apache and I use Virtual Hosts, because I manage more than one websites with my Server.

My httpd.conf looks like this:

  <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>

In Kohana every http request needs to go to the index.php first. Because I dont like these ugly URLs that all starts with index.php (for example www.myPage1.com/index.php/item/detail/itemId) I used the following .htaccess file which worked perfectly

# 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]

I would now like to not use a .htaccess file anymore and instead put all the rewrite logic into my httpd.conf file. The following gives me a "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>

What am I doing wrong? Help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苏大泽ㄣ 2024-10-12 08:21:00

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

If you are using mod_rewrite in a VirtualHost block with the REQUEST_FILENAME condition, you will not get the filename (this is per the documentation). Instead, this variable contains the same value as REQUEST_URI, which contains the path excluding the query string.

Quoting from the above-linked page:

If used in per-server context (i.e., before the request is mapped to the filesystem) SCRIPT_FILENAME and REQUEST_FILENAME cannot contain the full local filesystem path since the path is unknown at this stage of processing. Both variables will initially contain the value of REQUEST_URI in that case. In order to obtain the full local filesystem path of the request in per-server context, use an URL-based look-ahead %{LA-U:REQUEST_FILENAME} to determine the final value of REQUEST_FILENAME.

However, I have found that the suggested solution %{LA-U:REQUEST_FILENAME} does not work, at least not for the virtual hosts on my server. To be sure your RewriteCond lines function as expected in a VirtualHost block, you should prefix them with DOCUMENT_ROOT, thus:

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

This method will always work. The switch to _URI is in case REQUEST_FILENAME one day returns the actual filename, to avoid a double-prefixing of the document root.

酒儿 2024-10-12 08:21:00

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

You don't have a RewriteBase in your httpd.conf. And what's wrong with the .htaccess?

晨与橙与城 2024-10-12 08:21:00

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

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

Remember, if you put these rules in the main server conf file (usually httpd.conf) rather than an .htaccess file, you'll need to use ^/... ... instead of ^... ... at the beginning of the RewriteRule line, in other words, add a slash.

check this, may help.

喜你已久 2024-10-12 08:21:00

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

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

could it be this typo (maybe it should be 'application'):

RewriteRule ^(?:aplication|modules|system)\b.* index.php/$0 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文