buy linux on cd and dvd

Using Apache's Rewrite Engine with vhosts enabledUsing Apache's Rewrite Engine with vhosts enabled

Posted June 19th, 2007 in Apache

I am currently working on a project for a customer where I'm converting it from a static html site to a database driven site with a search engine and a consultant's name find function. I am using Codeigniter for this project, which uses Apache's mod_rewrite feature, but had some issues when I uploaded the project to the customer's web host due to it being set up with vhosts. Apache's rewrite engine allows urls to be rewritten from what appears to the browser to something different which Apache will actually process.

On my development machine this worked no problems with the following .htaccess file:

# start the rewrite engine
RewriteEngine On

# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteRule .* - [L]

# testing version of the site
RewriteRule ^testing(.*)$ testing.php$1 [QSA,L]

# redirect to our front web controller if nothing above matches
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

The useful .something rewrite condition means that any file with a dot in it will be ignored by the rewrite engine and left to Apache to attempt to serve it instead. Everything else will be run through the frontend controller in the index.php file.

When I uploaded this to the customer's web host (Web Drive), it wouldn't work, giving me an error like this:

The requested URL /httpd/vhlinks/[domain-name]/index.php/ was not found on this server.

where [domain-name] was the customer's domain name.

I spent a short amount of time searching for an answer. Web Drive use vhosts mapping (I'm not all that familiar with how this works, not having ever set up a web server in this way), but all I needed to do to make it work was to change the main Rewrite Rule to contain a %1 as follows:

RewriteRule ^(.*)$ %1/index.php/$1 [QSA,L]

I re-uploaded my changes and then it all worked fine.

Yahoo! Personals Yahoo! Autos