Reputation: 42173
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.
Upvotes: 3
Views: 1161
Reputation: 625447
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.
Upvotes: 4
Reputation: 29749
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]
Upvotes: 1
Reputation: 10685
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
.
Upvotes: 4