Setup site to automatically "create" urls using templates

I am a newb to programming, and have been able to put together a fully functional PHP/MySQL site (but since this is more of a “general question” I posted it here). I feel I did a good job at it. However, I want to take it to the next level.

When a user goes to a page, the page pulls content from the database (based on the query that I set for that specific page). This worked fine, but I have to create EVERY page/folder that is linked to, and this isn’t very efficient or user friendly. (even when using includes)

Example of folder structure
It might be best to show an example. Below is a website structure, where a ton of pages exists. Each page has a query relating to the information that it wants to pull from the database (Food, will pull ALL the food in the table; Apples, will only pull all the APPLE TYPES; and Red-Delicious will be a description page, containing only information relating to that specific row for Red Delicious Apples.)


What I do now…
As I have it now, I have to create a page and folder for EACH of these sections, and that is ALOT of work.


And, if there is an issue on one page, then I need to go through ALL the pages and make the revision. And, if the database categories change, then the entire folder structure that I use is messed up and has to be redone (with all the links)…

What I need to do…
I wanted to know if there was some way to use a master page (Foods/index.php) and have it used as a base for all the other pages/folders. And, maybe doing something with htaccess to point to those folders.

So, the htaccess may be something like… (below is what I want to do, and certainly not the code I would use… since I don’t know how to do this)
Redirect Foods/index.php?Cat1=Fruits Foods/Fruits/
Redirect Foods/index.php?Cat1=Fruits&Cat2=Apples Foods/Fruits/Apples
Redirect Foods/index.php?Items=Red-Delicious Foods/Fruits/Apples/Red-Delicious

Real life example:

When you click on an individual spell, it goes to the individual spell page…

The individual page is NOT a page that the author created. With over 400 spells, I’m pretty sure the server somehow redirects the URL to a template page.

In Closing
Once again, I am new to programming and not very proficient with server-lingo. So, if you can point me to a tutorial, or tell me what I may need to ask my webhost, that would be VERY helpful. Or, try and use small words so I understand. :grin:

Let me know if you can help. Thanks.

this is some htaccess that I used to create a site last year:

RewriteEngine On
#rewriteRule only applies to items that are not files or folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule  ^(.+)$ index.php?url=$1 [QSA,L]

If you use this in your htaccess, you can construct urls with / to split the parameters. This is received as a get parameter in php. You can split that string and parse into anything you want, then echo the result.

The URL could be constructed as and will be received in php $_GET[‘url’] as “/Class/Family/Name/Variety/Colour”. There might be a trailing slash, I can’t remember.

Cool. This works to “rewrite” the paths. But there is a minor issue…

Here’s what works:
If I go to …
and the rewrite is set for…

It will show the correct content, as long as I set the conditional statement in the index.php page …

$pagepath = $_GET['url'];
if($pagepath == '/Foods/Fruits/Apples/Red-Delicious') { 
     /* Show Content */  

Here’s what’s wrong…
But, if I go to any not-designated directory in that folder, it shows the default index.php page…

/Foods/Cars/Elevator/Fun/Website/ (or ANY url that I don’t designate)
will show content from …

I’d need undefined paths/pages to get a 404 Page Not Found error.

In addition…
Does this also account for URLS with other variables?

So, if the url is …
The rewrite should have the URL read /Foods/Fruits/ …
but does it also pass the variable letter (If I use the _GET[‘letter’]) as well?

you could set up conditions for each parameter in the string. First, split the string into an array, then decide what to show based on the first element, then the second etc.

for example, if you choose /Foods/Fruits/Apples/Red-Delicious, the first element is Foods. You can set up the same if statement and, in the case of foods, just call another file with all your food logic and send the rest of the elements to decide what to do with food.

The same would apply to letters, just use that as the first word before the slash and set up a condition for it that leads to another file handling the rest of the parameters.

Yes you would. Add a variable (for example $foundPage) and set it to true if the first condition is met, be it letters, foods, or anything else you care to consider. If, at the end $foundPage is still false, set the 404 header and send whatever output you want for your 404 page.

You would really benefit from doing this php tutorial that I learnt from, Completing your project using the MVC pattern will make everything easier for you.

This got complicated fast. Not sure I will need to split the string into an array. I think I’m going to simplify the category list a bit. So, I can setup a few conditional statements for each category. Not sure how to do it though…

So, I’d need a conditional for …
/CategoryAlpha/ wwwsitecom/page?cat=1
/CategoryBeta/ wwwsitecom/page?cat=2
(so, anyone going to CategoryAlpha would see the content from page?cat=1)

I think setting up the category pages shouldn’t be too hard… but I’d like to use a designated path for each individual item “pages”. So instead of…
… I’d rather just have that item/row page show up as /thisitemspagepath/
Is that possible without having a “rewrite rule” for EVERY item in my table?

My goal is to have a main page, listing the table, and have links to the individual item pages (). But, all the individual item pages are actually one page (i.e. /page?url=red-delicious&id=234 ). I’m trying to avoid links with variables in them.

I hope I explained this well…