views:

96

answers:

3

I was thinking of setting up my website with a file called master.php and using .htaccess and URL rewriting I would transform

http://mysite.com/About into http://mysite.com/master.php?selected=About

Which would allow me to setup the about page. Is it a bad idea to have one master page that creates dynamic pages?

+6  A: 

No, this is fine. It is called a Front Controller pattern.

Just make sure you direct only page requests this way, and not requests for images, style sheets and other static resources.

Pekka
A: 

That's completely OK, but make sure you don't transform /images/test.png into /master.php?selected=images/test.png, which can be a huge bug.

SHiNKiROU
You can handle it correctly with very little effort (detect if `$_GET['selected']` is the path to a real file and spit out the file if so). In some cases, this is desired, ie. to do image manipulation on the fly. Resizing and such of user-provided images in a CMS would be an example.
Matthew Scharley
Using `RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d` in the .htaccess file will prevent the rewrite of existing files/directories to the master.php controller.
pygorex1
A: 

I actually use this .htaccess (I use the codeigniter framework, which uses a front controller pattern):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

So as long as the file or directory does not actually exist, then it's going to get routed through index.php

And actually it's a good design pattern. It actually is easier to maintain in some ways, seeing that urls only have one entry point.

Matthew