Close and Go BackBack to Viget

Quick Apache Rewrite Rule for MVC Apps

Patrick Reagan
Patrick Reagan, Development Director, June 14, 2006 0

Josh Schachter, at the Future of Web Apps Summit, described mod_rewrite as both a necessity and a “dark art.” For today’s crop of MVC-framework web applications, it’s difficult to create user-friendly and hackable URLs without it. Here are a couple rules that make it easy to direct all requests to a single controller:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*).html$ index.php?request=$1 [QSA,L]

This will essentially push all files with an .html extension through your front controller. But, what if you have a file with this extension that you want to serve up? Just check to see if the file exists before redirecting to the controller:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).html$ index.php?request=$1 [QSA,L]

This essentially replaces the O’Reilly tutorial about creating a front controller, and it even works in Apache 2!

Trackback URL: http://www.viget.com/trackback/804/

Comments for this entry were closed after 60 days.

Next entry: Throwing a Boomerang

A Development Community for Viget Labs and Beyond

Every team member here at Viget Labs strives to be an innovator. We members of the development team are no different - that's why we're constantly engaging in community discussions and exploring the unknown that is the next generation of open-source web applications.

Viget Is Hiring!

Viget has job openings for Ruby Developers, Interns, and Front-End Developers. Learn More »

Recent Comments

I think that polymorphic_url(@commentable, :anchor => “comment_#{@comment.id}") should work. You can also refactor the “comment_#{@comment.id}” to a separated method, like dom_id, which returns the dom identifier of the comment.