Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialorange sky
Front End Web Development Techdegree Student 4,945 Pointslocalhost displays index.php or index.html
Hello!
In the lecture video, the instructor said that most browsers are configured to display index.php or index.html
Let say in my htdoc, I have a folder called report and and index.html file in that folder
1) When I type in the the address bar, localhost:80/report/ Does the browser send a request to the server for either index.html or does it automatically look index.html or index.php without making a request to the server?
2) If I understand, the server will only process php code, and return the processed file to the browser, but what if the index.html , that is supposed to automatically display when the browser loads, has php block code in it.
Thanks
4 Answers
Chris Shaw
26,676 PointsHi orange sky,
Browsers don't determine the document type as that would create far too much room for something to go wrong as it would only take one small bug to cause utter chaos for a website that works correctly in one browser and not the other, what actually controls the type of document you see visually is the web server itself whether it be Apache, Nginx, IIS etc.
All servers use different software but a common implementation between all of them is the default document type which is specified in the servers configuration and that's what's used to serve the correct document to the browser, the way this config in Apache for example works is as follows.
DirectoryIndex index.html index.php
What this will do is search the current working directory or the root folder for the website and look for index.html, if it can't find that file it will then try to find index.php and so on, if none of these files exist on the server then either a directory index will take place which lists out all the files/folders in the working folder otherwise by default a 403 forbidden error will occur.
Hopefully that answers that.
but what if the index.html , that is supposed to automatically display when the browser loads, has php block code in it.
Static HTML files should NEVER contain PHP code, you should always parse PHP files using their respective .php file extension and serve HTML through them but not the other way around.
orange sky
Front End Web Development Techdegree Student 4,945 PointsHey Chris,
Sorry for sounding like I know you, but I do remember you used to answer my questions quite well.
In fact, thank you for your clear and thorough answer above; just one question
"All servers use different software but a common implementation between all of them is the default document type which is specified in the servers configuration.... "
1) Is the default document type that is common to most servers called DirectoryIndex(or is this just for example)?
Lets say in my htdoc, I have a folder called inc and in the folder called inc, I have a file called about.php.
I know that the way to request for a file is by: localhost:80/inc/about.php. When would I have ever had to use default document type? I think I don't need to type it in the address bar, but when can use it.
Sincerely yours
Chris Shaw
26,676 PointsIs the default document type that is common to most servers called DirectoryIndex(or is this just for example)?
This is specific only to Apache, I used it as an example because Apache is the most common HTTP server used for hosting websites.
Lets say in my htdoc, I have a folder called inc and in the folder called inc, I have a file called about.php.
I know that the way to request for a file is by: localhost:80/inc/about.php. When would I have ever had to use default document type? I think I don't need to type it in the address bar, but when can use it.
Default document types are really only necessary for files that load the default file for each parent and child level folder, typically servers are setup by default with index.php as the initial document file to look for; followed by index.html and so on.
As a developer the only time you would ever set your own default document type is if your initial file was called something like main.php which I've seen before but to many experienced developers index.php makes the most sense.
Hope that helps.
orange sky
Front End Web Development Techdegree Student 4,945 PointsHello Chris,
I am sorry for this one; I just realized that I have confused myself.
Ok, let's use my web server to break things down to my level :)
I have XAMPP installed and it has an Apache web server. I have alway thought the root directory folder for Apache was localhost, but now you mention DirectoryIndex, and uh, I think I need to ask first the difference between localhost and DirectoryIndex
Cheers!!
Chris Shaw
26,676 Pointslocalhost
The domain (localhost) or TLD (top level domain) as it's known refers to the Document
reference in your Apache httpd.conf file, the domain itself is a reference that Apache uses to determine which folder it should be loading files from which is known as the root web folder or htdocs in your case since you're using XAMPP.
DirectoryIndex
This is simply a space separated list that tells Apache which file to look for first and load by default if you don't specify a file at the end of your URL, for instance if our DirectoryIndex
was the following:
DirectoryIndex index.html index.php
And our URL was:
Apache would go off and look for an index.html
file in our root web directory first and if it can't find that it will then search for an index.php
file, if it doesn't find either of these files as I explained above it will throw a 403 forbidden error depending on your servers error handing configuration.
However, if we specified the following URL Apache would ignore the DirectoryIndex
since we have given it an explicit file to find and load for us.
Hope that clears up any confusion around the two.
orange sky
Front End Web Development Techdegree Student 4,945 PointsHello Chris,
Big thanks always for your clear and easy to understand explanations.
Cheers!