Mod_rewrite special case front controller

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Mod_rewrite special case front controller

Post by matthijs »

For my front controller I use these lines in my htaccess file:

Code: Select all

RewriteEngine on
RewriteBase /
RewriteRule !\.(php|htm|html|cgi|js|ico|gif|jpg|png|css)$ index.php
So that all requests which are not existing files are handled by the index.php (front controller).

However, say you want to make an exception for a specific directory. Say /blog/ in which you install a blog application, which uses it's own htaccess file and rewrite rules. Then the htaccess rules above prevent that from working. Now I found out that adding this:

Code: Select all

RewriteEngine on
RewriteBase /
RewriteRule ^blog/(.*)$ blog/$1 [L]
RewriteRule !\.(php|htm|html|cgi|js|ico|gif|jpg|png|css)$ index.php
Will make sure that each request for /blog/{anything else} will be handled by the htaccess in the directory /blog/

Is this the/a correct way to handle a situation like this?
(it seems to work, but I'm just wondering if there are better solutions)
Post Reply