Apache:用于Kohana Framework的httpd.conf中的虚拟主机和URL重写

时间:2022-09-17 16:52:28

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.

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

My httpd.conf looks like this:

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

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

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

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"

我现在不想再使用.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>

What am I doing wrong? Help would be appreciated!

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

4 个解决方案

#1


2  

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

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

#2


3  

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.

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

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:

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

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.

这种方法总能奏效。切换到_URI以防REQUEST_FILENAME有一天返回实际文件名,以避免文档根目录的双重前缀。

#3


1  

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.

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

check this, may help.

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

#4


1  

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

可能是这个错字(也许应该是'应用'):

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

#1


2  

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

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

#2


3  

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.

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

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:

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

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.

这种方法总能奏效。切换到_URI以防REQUEST_FILENAME有一天返回实际文件名,以避免文档根目录的双重前缀。

#3


1  

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.

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

check this, may help.

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

#4


1  

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

可能是这个错字(也许应该是'应用'):

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