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!

We're the Developers

at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.

Recent Comments

For translating strings you can use Rails I18n backend instead of using inflectors.

The `typus_human_name` is a patch to fix a problem in `human_name` [1].

[1] https://rails.lighthouseapp.com/projects/8994/tickets/2120-humanize-and-human_name-dont-separate-words

Contact Us

Have any questions, comments, ideas, or secrets to share? Let us know.


Sorry, you need to have Javascript enabled to use this form. (Don't blame us, blame the spammers!) If you'd like to contact us, please visit our Contact page.