Pascal Klein
Pascal Klein

Reputation: 24873

Apache: Virtual Host and URL Rewriting inside httpd.conf for Kohana Framework

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!

Upvotes: 0

Views: 14401

Answers (4)

Nicholas Shanks
Nicholas Shanks

Reputation: 10981

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.

Upvotes: 4

Raphael
Raphael

Reputation: 11

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

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

Upvotes: 1

Pengfei.X
Pengfei.X

Reputation: 681

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.

Upvotes: 1

Dorian Sarnowski
Dorian Sarnowski

Reputation: 146

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

Upvotes: 2

Related Questions