My server only contains some files like home.php
, this.php
, that.php
, etc. But when I type my website (example.com
), how does it show the contents of example.com/home.php
?
-
See also: Is it the responsibility of the web client or server to use the directory index file for the home page? and What's the purpose of index.html and How to I get my server to autoload my index.html?– Stephen Ostermiller ♦Commented Nov 7, 2019 at 11:12
-
I edited your question to remove "redirect". A redirect changes the URL that is visible to the user. In this case, the contents of the file are shown at the base URL, which is not a redirect.– Stephen Ostermiller ♦Commented Nov 7, 2019 at 11:13
1 Answer
The home page for a website is served based on the configuration of the web server that hosts it.
Typically, the default setting would be that on attempting to reach the root of any directory, the server would attempt to serve a file named index, either index.html, index.php, or index.asp (this is generally down to the preference of the person building the website, the software and frameworks installed on the server, or the CMS being used). The exact name of this file can usually be changed in the web server's configuration.
If you had, for example, both index.php and index.html in the same directory, then the server's configuration again would decide which one took priority and was shown.
If there is no file named index, then either an error is returned or a listing of the files and subdirectories within that directory is shown (although this is seen as outdated behaviour and many web servers disable this feature by default as it can be a serious security risk, giving any visitor full access to the file structure isn't a good idea).
A typical web server for a beginner (and even a far more advanced developer) would be running a LAMP configuration, this would usually look for an index.php file as the default first. If you set up a WordPress website on this server, you'd see that it includes an index.php. The entire public facing side of the website would be actually be directed to that file, with different content translated into instructions for it to load the different "pages" (actually just data stored in a database).