Page 1 of 1

Mod_rewrite special case front controller

Posted: Sun Nov 11, 2007 3:43 am
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)

Posted: Sun Nov 11, 2007 5:10 am
by phpBuddy
I think you can use this.

I do not know any better way, but this is because I do not know and use Rewrite.
Why not?
Why not use PHP scripts that depend on Rewrite?

Well.
1. What will happen when you want to use your code,
in a webserver with. htaccess, but where Rewrite Engine is OFF?
2. What will happen when you want to use your code,
in a webserver without .htaccess?

Your script will not work.
You will have rewrite your code. Or get another webserver.
So, depend on Rewrite will limit your options.

Regards

Posted: Sun Nov 11, 2007 3:17 pm
by matthijs
Well, using mod_rewrite is very powerful and elegant, so I will always make sure any host I use does has mod_rewrite installed. Haven't come across one which doesn't, by the way.