How do I duplicate this folder structure for php pages

I am very new to coding, so I am taking on a big challenge with my web page. The page consists of dynamically created content pulled from a database. My site is a photo gallery site that displays images that all belong to sets.

Depending on where you are in the hierarchical menu, I want the site to either display a gallery of the set, or if an image is clicked on, then I want it to display the details regarding the exact image.

I have already created all of this, but I am not happy with my page navigation.

I want to duplicate the navigation as what is show in this sample site below:

Example #1

https://www.miniscollector.com/pathfinder-battles/pathfinder-battles-premium-painted-figures

The URL above, directs you to a gallery display of all images that belong to the “pathfinder-battles-premium-painted-figures” set.

Example #2

https://www.miniscollector.com/pathfinder-battles/pathfinder-battles-premium-painted-figures/human-rogue-male

The URL above directs you to a page containing specific information on a miniature.

Could someone please tell me how this can be done. I’m only used to creating pages that end with an extension (i.e.: .php).
Example:

https://www.minisgallery.com/index.php?id=pathfinder-battles-premium-figures-wave-1

The server reads PHP pages and process eventual php commands and then they are served as HTML so if you create a static page (without php commands) it could work with either extension.

So I’ve moved your topic to the HTML&CSS forum where I think this belonge. :slightly_smiling_face:

Could you elaborate a little, the nav seems to be the same already.

Are you asking how to include the nav from a separate file to more pages through a PHP command?

If so, the topic should go back to the PHP forum. :thinking:

Please note: I am not a coder, so I may be using coding terms incorrectly – sorry for any confusion by this.

Currently, my page navigation is done by this:

https://www.minisgallery.com/index.php?id=pathfinder-battles-premium-figures-wave-1

Since I don’t know how to code properly for building a hierarchical navigation menu system, I have some manual components in how I create it. I have a page where I assign whether a label (i.e. pathfinder-battles-premium-figures-wave-1) is a menu page or a set gallery, and store this setting in the database.

In my php code, I GET the id “pathfinder-battles-premium-figures-wave-1” and look to see in my database if I have declared this as a “menu” or “gallery”. Depending on the selection, it then knows what table in the database to pull the information from.

As for my image handling, its not great right now. Currently my images are solely stored by assigning them an image #. In example, to generate the dynamically created image display page, here is the URL used:

https://www.minisgallery.com/index.php?id=5506&task=image&imageName=Elf%20Fighter%20Male

All of my images are however stored in set folders in my image folder:
The image folder for the above is:

https://www.minisgallery.com/images/minis/pathfinder-battles/premium-figures/male-elf-fighter_pre.jpg

Now, getting back to what I am trying to achieve.

In example #1, a “set” is being referenced, so a gallery page is being displayed:

https://www.miniscollector.com/pathfinder-battles/pathfinder-battles-premium-painted-figures

There is no .php extension. How does this still work? The level of coding that I am at – all pages end with an extention (.php / .html) This does not. Further more, it knows that “pathfinder-battles-premium-painted-figures” is to display a gallery page.

In example #2, a specific item is being reference, so a page showing information of that item is being displayed:

https://www.miniscollector.com/pathfinder-battles/pathfinder-battles-premium-painted-figures/human-rogue-male

Again, how can I do this, without showing an extension?

Ultimately I am trying to achieve better search results. A URL like this shows up on a browser search:

https://www.miniscollector.com/pathfinder-battles/pathfinder-battles-premium-painted-figures/human-rogue-male

Where-as this obviously does not:

https://www.minisgallery.com/index.php?id=5506&task=image&imageName=Elf%20Fighter%20Male

With regards to image handling, I do realize that in my image database I am going to have assign all my miniatures a label name (i.e. elf-male-fighter).

I just need some direction on how any of this can be achieved without show extensions. (Its not that I don’t want to show the extensions – its more that I can’t figure out how to make any of this work without using extensions.

Note: (I make reference above to not showing extensions, but I actually mean not showing the page reference itself ‘index.php’)

This sounds like a question about redirects.
What type of server are you running on? Is it Apache?

Yes, it is Apache.

So this is something that will be done in your .htaccess file.
…and off it goes to the “Server Config” section.

1 Like

I suspect I have not explained myself clearly. Sorry, as I am not a programmer.

Lets use a specific example:

https://www.minisgallery.com/index.php?id=5504&task=image&imageName=Human%20Rogue%20Male

I have now added a column on my database called “mini-path” which records the value that I want to appear as the path. In the above example I have added “human-rogue-male” as the value in this column for mini id# 5504.

How do I get “human-rogue-male” to show on the URL, instead of “index.php?id=5504&task=image&imageName=Human%20Rogue%20Male”

If this does involve the .htaccess file, can you explain a bit more please, as I have no knowledge of .htaccess files other than knowing that they exist.

I’m prepared to redo my whole dang site just to get his working – please advise how I should ideally structure this to make it work.

To help explain my confusion here. I want people to be able to click on the URL “www.mysite.com/pathfinder-battles/premium-painted-miniatures/human-rogue-male” and be directed to the image page of this miniature.

If I am able to use: www.mysite.com/pathfinder-battles/premium-painted-miniatures/human-rogue-male as my URL, how do I build this logic into PHP:

  1. Get the last path from the url (which is this case is “human-rogue-male”)
  2. Recognize that “human-rogue-male” is a miniature I am looking for, and not a set (“premium-painted-miniatures”) or other menu category (“pathfinder-battles”)

After spending some more time banging my head against the wall, I think I may understand what I need to do using .htaccess:

RewriteEngine on
Rewrite Base /
RewriteRule ([a-z]+)/?$ index.php?path=$1 [NC,L]

I need to structure my page so that all my pathing defaults a value to target:

For my rogue mini, to get the desired URL output of:

www.mysite.com/pathfinder-battles/premium-painted-miniatures/human-rogue-male

This needs to become:

www.mysite.com/index.php?path=pathfinder-battles/premium-painted-miniatures/human-rogue-male

So then I need to write PHP code to break up those paths into separate variables that can figure out where and what I am trying to reference. (i.e. a menu / gallery / set / mini).

To me the easiest way to do this would be to include a “target” tag on my URL: “&target=mini”, however, how could I then incorporate this into the re-write to register $2 as the value of the “target”?

Here’s a link to a fairly well written beginner’s guide - https://aloneonahill.com/blog/url-rewriting-for-beginners/

There are two sides of the fence when deciding what the url should look like. 1) That this forum software uses, is partly using text fields (the community and the t parts) and in addition to putting the pretty thread title text in the url, is also including the corresponding id values, e.g. the 358548 is the thread id and the /x is the reply number, and 2) Leave the numerical ids out of the url, which the url of the blog I linked to is using. The first version is faster, because the pretty thread title text is completely ignored and the numerical ids are gotten from the url and passed to the main php file that receives the request and then queries to find the matching content. The second version, has a simpler url, but takes longer to execute since you are doing a text based query search to find the actual content to display.

I see you have researched this and posted while I was writting the above.

That’s a third side of the fence to consider. Most people would do this through rewrite rules only, i.e. the php file receiving the request would receive a set of get parameters, that it would just use, rather than to split this between some rewrite rules, then have php breakup and parse the remainder of the url itself.

1 Like

I have done some reading on this, and as can be expected everyone seems to write rules differently. Based on some tutorials I have now gone through, here is currently where I am at:

RewriteEngine On

# If using an old link to a file or directory then do not rewrite
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f

 # Rewrite URL when it contains a $path and $target
RewriteRule ^([A-Za-z0-9-])$ index.php?$path=$1&target=$2 [NC, L]

I have seen a couple site though advise that when using multiple parameters, that you should use sort of a reference chain such as:

RewriteRule ^([A-Za-z0-9-])$ index.php?$path=$1 [NC, L]
RewriteRule ^([A-Za-z0-9-])/([A-Za-z0-9-])$ index.php?$path=$1&target=$2 [NC, L]

I don’t quite understand this, because if I comprehend this correctly, that first rule is going to be the stopping point because it will be true and it contains the “L” attribute?

You would swap the two lines, so that the ‘harder to match’ rule comes first. If the url doesn’t match that rule, it would then continue and try to match the second, more general rule.

I run an Apache Server and prefer to use the following very simple .htaccess file that redirects every URL to index.php. It is easier to test the URL parameters using PHP and apply the relevant instructions:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Please note there is a good chance the redirection will not work if Apache is missing certain include files.

That’s something I’ve not thought of before. But I see how that could simplfy the rewrite rules, which can get a bit complex with multiple parameters, expressed like sub-directories like this.

I’m thinking it could potentially start with something along these lines.
Where the URL you are directed to is like:-

www.example.com/index.php?params=this/that/other

Which can probably be dealt with, with a single rule in .htaccess.
Then your PHP starts like:-

$params = explode('/', $_GET['params']);

Instead of a load of GETs, then PHP works out the routing from there.

Thank you for this. I think between using this, and SamA74’s example with the params, I should be able to get things working.

I’ve been looking over a few more tutorial sites and all seem to miss explaining what actually happens at the first part of the rule.

So in this example, I do understand that:

  1. ^ and $ mark the start and end.
  2. . selects any non-whitespace characters
    3 * the previous character can be matched an unlimited number of times

Question 1:
Its not clear to me though, is what part of the URL ^(.*)$ is referencing? Is it referencing the entire URL, or only the portions after the main URL (root? - not sure I’m using that term correctly) - which would be “www.mysite.com” ?

Question 2:
So, would the above expression then take anything after the “root”, such as “www.mysite.com/pathfinder-battles/promos/knight” and then using the ^(.*) remove everything after “.com” and place it into the $1 variable, would then result in: “www.mysite.com/index.php/pathfinder-battles/promos/knight” ?

Question 3:
If I want to use SamA74’s param’s method, would my rule then look like this?:

RewriteRule ^(.*)$ index.php/params=$1 [L]

Question 4:
If I am understanding this correctly, doesn’t ^(.*)$ also capture the leading “/” from “/pathfinder-battles/promos/knight”?

Question 5:
Should this be removed when placing it into $1?

It is worth mentioning the first two rules test for a specific filename or directory. If not true then third rule extracts all parameters.

Try numerous URLs then in index.php examine the following output:

<?PHP
echo '<pre>'; // add line feeds
print_r( $_SERVER );
echo '</pre>';
exit;

Edit:

I forget to mention that instead of pasting the above into index.php, create a new file called something like test-001.php, change the .htaccess script and save both files to the online server. Everything should work ok even the original index.php.