How to deliver content for a directory without any files inside?

In most big dynamic websites, directory default pages are not simply index files but some sort of thing I don’t know. For example, this url in Reddit (https://www.reddit.com/wiki/wiki/) does not have one. If I go to …/wiki/wiki.html, …/wiki/wiki.php, …/wiki/wiki/index.html, etc., they are all separate pages from the default page.

How is that, and how do I do that for my website? So for example, if I have a url (https://imagienworld.rf.gd/example/, which is an empty folder (/htdocs/example), but when a user accesses the url, they are delivered a php file from another directory out of the example folder.
To my understanding, this is how a dynamic webpage is built. So how do I do it?

So you want (for example) the file /files/file2.php to be shown to the user when they visit /example?

That’s not really recommended, and its really confusing to maintain and edit files in the future.

not exactly. A dynamic website is a website that customizes each page for each user. For example, if you go you youtube.com, that’s a dynamic website because every user is going to see something different depending on the videos they watched before. But if you go to a site like tinkertechlab.com, that would be a static site, since the same page is shown to every user.

Basically, if you login to a website, its dynamic, and if you don’t, its static (In general).

It’s possible with .htaccess, but again, I highly recommend not doing that (Unless I am mis-understanding you).

Instead, create an index.php or index.html file in the example directory, and that is the page that will show when a user visits /example.

3 Likes

How did Reddit make it a separate page when you visited reddit.com/wiki/wiki/index.html, but not reddit.com/wiki/wiki? If Reddit is using some super long string as …/wiki/wiki’s default page, for example “…/wiki/wiki/agoqqotqjgqigqjgq.html”, then how can I find out that super long string, before their .htaccess redirects me to a url without it?

Most websites make custom URLs from the application code, not through files and directories. For example, the server this forum runs on doesn’t have a directory t that contains a folder how-to-deliver-content-for-a-directory-without-any-files-inside that holds the data for this website.

Instead, the server is configured that it will try to find a file or folder with the name of the URL. And if that doesn’t exist, the request is sent to the website code (usually a single index.php file for PHP websites). The index.php file then runs code that parses the original URL, and loads a page depending on the original URL.

You can build this yourself by writing code that parses the $_SERVER['REQUEST_URI'] variable, and a htaccess file that looks roughly like this one from WordPress.

But you can also use a PHP framework instead to handle this common functionality. Every web framework has functionality for this.

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.