Mod_rewrite special case front controller

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

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)
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Post 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
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post 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.
Post Reply