Using Apache's Rewrite Engine with vhosts enabled
Posted June 19th, 2007 in Apache (Updated April 21st, 2009)
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]
# 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.
Update April 21st 2009: The above rewrite rules were for CodeIgniter. Today I used a similar set of rules to the above on the same hosting provider and using a different PHP framework. The original rewrite rule I tried, modelled on the above, looked like so:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(css|gif|ico|jpg|js|php|png|swf|txt)$ %1/foo.php/$1 [L]
However I kept getting what appeared to be entirely random blank pages. After about an hour testing various other different PHP files etc (which all ran just fine) I finally managed to work that in this instance the /$1 at the end was causing the issue.
Revising the rule to the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(css|gif|ico|jpg|js|php|png|swf|txt)$ %1/foo.php [L]
fixed the problem and random blank pages stopped.
Related posts:
- Compressing files on Apache with mod_deflate (Tuesday, November 6th 2007)
- Default virtualhost's secure certificate used with mod_ssl (Friday, September 21st 2007)
- Howto Restart Apache (Wednesday, December 22nd 2004)
- Virtual Hosts and Secure Certificates (Sunday, November 30th 2003)
Share or Bookmark
Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.
Subscribe or Follow
Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.
