Apache:Kohana 框架的 httpd.conf 内的虚拟主机和 URL 重写
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在带有
REQUEST_FILENAME
条件的VirtualHost
块中使用 mod_rewrite,您将无法获得文件名(这是根据 文档)。相反,此变量包含与REQUEST_URI
相同的值,其中包含不包括查询字符串的路径。引用上面链接的页面:
但是,我发现建议的解决方案
%{LA-U:REQUEST_FILENAME}
不起作用,至少不适用于虚拟主机我的服务器。为了确保您的RewriteCond
行在VirtualHost
块中按预期运行,您应该在它们前面加上DOCUMENT_ROOT
前缀,因此:此方法将始终有效。切换到
_URI
是为了防止有一天REQUEST_FILENAME
返回实际文件名,以避免文档根的双重前缀。If you are using mod_rewrite in a
VirtualHost
block with theREQUEST_FILENAME
condition, you will not get the filename (this is per the documentation). Instead, this variable contains the same value asREQUEST_URI
, which contains the path excluding the query string.Quoting from the above-linked page:
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 yourRewriteCond
lines function as expected in aVirtualHost
block, you should prefix them withDOCUMENT_ROOT
, thus:This method will always work. The switch to
_URI
is in caseREQUEST_FILENAME
one day returns the actual filename, to avoid a double-prefixing of the document root.您的 httpd.conf 中没有 RewriteBase。 .htaccess 有什么问题?
You don't have a RewriteBase in your httpd.conf. And what's wrong with the .htaccess?
请记住,如果您将这些规则放在主服务器 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.
可能是这个错字(也许应该是“应用程序”):
could it be this typo (maybe it should be 'application'):