tags:

views:

80

answers:

3

I am working on building an internal CMS for clients. Instead of creating a new php file for each page, I am wondering if there is a way to load up a page based on the URL but not a physical php file in that location.

So, if I visit www.mysite.com/new-page I would like this to just be a reference to my template, content, etc. rather than an actual .php file.

Sorry if I have not explained this correctly, but I am having a hard time explaining it.

Thanks.

+4  A: 

Sounds like you need the Front Controller pattern.

Basically every URL gets redirected to one PHP page that determines what to do with it. You can use Apache mod_rewrite for this with this .htaccess:

RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php

That redirects everything except static content files to index.php. Adjust as required.

If you just want to affect the URL /new-page then try something like:

RewriteEngine on
RewriteBase /
RewriteRule ^new-page/ myhandler.php

Any URLs starting with "new-page" will be sent to myhandler.php.

cletus
Yes, this does sound like what I am looking for. But, I don't really want all pages do be served from the index page. I still want URLS like www.mysite.com/page/page2 and www.mysite.com/page-test. Will the example you gave account for this?
Nic Hubbard
yes, with proper configuration
Here Be Wolves
+4  A: 

It will normally be the web server that is handling this for you in conjunction with your PHP code. So, for example, if you were using Apache you could use mod_rewrite to do something like:

RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]

And then in your php code you can check $_GET['page'] to see what page is being called.

So visiting mysite.com/page/blah would really access index.php?page=blah.

mopoke
+1  A: 

To use mod_rewrite to rewrite the URLs in the one of a PHP page is the solution. I would rather use a more generic rewrite rule that rewrites any URLs that don't take to a file of a directory existing on the server.

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
kiamlaluno