0

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!

Pascal Klein
  • 23,665
  • 24
  • 82
  • 119

4 Answers4

4

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.

Nicholas Shanks
  • 10,623
  • 4
  • 56
  • 80
2

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

  • I thought I just need a RewriteBase when I use URL Rewriting per directory with a .htaccess file. If my RewriteBase is like "/" I cant event start apache and get an error. I dont want do use the .htaccess file anymore because I want to have all settings central in my httpd.conf – Pascal Klein Dec 02 '10 at 19:32
  • 1
    There are lots of things wrong with `.htaccess`, the main one being an extra disk access for every directory under the document root in the request path, on each request. Your server will run considerably faster if you turn scanning for such files off (`AllowOverride None`). – Nicholas Shanks Jul 25 '13 at 07:05
1

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

RewriteRule ^(?:aplication|modules|system)\b.* index.php/$0 [L]
malenkiy_scot
  • 16,415
  • 6
  • 64
  • 87
Raphael
  • 11
  • 1
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.

check this, may help.

Pengfei.X
  • 631
  • 6
  • 9