Jump to content

Setup site to automatically "create" urls using templates


Recommended Posts

I am a newb to programming, and have been able to put together a fully functional PHP/MySQL site.  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.)
/Foods/
/Foods/Fruits/  
/Foods/Fruits/Apples/
/Foods/Fruits/Apples/Red-Delicious 

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. 

/Foods/index.php
/Foods/Fruits/index.php
/Foods/Fruits/Apples/index.php
/Foods/Fruits/Apples/Red-Delicious/index.php

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:
https://www.dnd-spells.com/spells/
When you click on an individual spell, it goes to the individual spell page...
https://www.dnd-spells.com/spell/aganazzars-scorcher   
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.  😁 

Let me know if you can help. Thanks.

You're trying to avoid having to keep track of every single thing/fruit, right? That's the problem you want to avoid. So a solution that involves having to track every single fruit probably isn't the right thing to do.

Look into URL rewriting. What you will do is tell your web server (Apache) that you want to match a URL pattern and turn it into something else. The pattern looks like /Category/Subcategory1/Subcategory2/Item/Page, but I can't tell whether those three categories are fixed (always three) or not.

But before you get there, you need a PHP page that can look up what it needs given the path. Like the Foods/index.php examples, except you should make the "Foods" generic too. As in

index.php?cat=Foods
index.php?cat=Foods&cat1=Fruits
index.php?cat=Foods&cat1=Fruits&cat2=Apples
index.php?cat=Foods&item=Red-Delicious

When you make that work, then you can worry about setting up the URLs to work with the nicer-looking versions.

However, if the three categories are not fixed then you have to do something different...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.