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 trial

PHP Building Websites with PHP Slim Basics & Twig Templates Including & Rendering

Dean Friedland
Dean Friedland
2,453 Points

Get 404 error when trying to access index.html and contact.html

When I click on the "preview workspace" icon the emerson page loads just fine, but if I click on either of the top heading buttons (index.html or contact.html links) then I get a 404. Please help.

Here is my code:

<?php
require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set ('America/New_York');

//$log = new Monolog\Logger('name');
//$log->pushHandler(new Monolog\Handler\StreamHandler('app.txt', Monolog\Logger::WARNING));
//$log->addWarning('Foo');

$app = new \Slim\Slim();

$app->get('/', function() use($app){
    $app->render('index.html');
});

$app->get('/contact', function() use($app){
    $app->render('contact.html');
});

$app->run();

8 Answers

Logan R
Logan R
22,989 Points

I think he says in this video or the next that the header is no longer correct and what you are experiencing is normal.

When you go to localhost/index.html, it's being directed to the index.php and looking for route /index.html, but if you notice, there is no route called that. The link in the header needs to be changed to reflect the new routes in order for them to work.

Dean Friedland
Dean Friedland
2,453 Points

Hmmmm. He does not say it in this video. Maybe the next. I have been stuck on this for over an hour so a heads up from the Treehouse staff would have been nice.

I have literally looked at his code and and compared it with mine over 20 times and have re-watched the video a bunch of times as well.

Can you dumb your explanation down a bit and explain how to make it work? Meaning how and where do I change the header and what do I change it to? Thanks Logan

Logan R
Logan R
22,989 Points

If you want to change it, on the index.html on line 56,

<a href="index.html" class=selected">About</a>
<a href="contact.html">Contact</a>

Change index.html to / and it will fix the problem. Same for contact. Change it from contact.html to contact.

<a href="/" class=selected">About</a>
<a href="contact">Contact</a>

So lets take a look at what's going on. If you remember, we created a .htaccess and pasted some code in there. Basically, all the code did was redirect all traffic to index.php and pass in where you wanted to go.

For instance, if I do localhost/contact, it will go to index.php and pass it that I was trying to go to /contact. If we take a look at our app,

$app->get('/contact', function() {
    $app->render('contact.html');
}

This is saying 'hey, if you see someone is looking for /contact, send them what ever is in the contact.html page.

If you want to try it out on your own, instead of changing the HTML in the header, add a new route that goes to /contact.html.

$app->get('/contact.html', function() {
    $app->render('contact.html');
}

Does that help you out at all?

Dean Friedland
Dean Friedland
2,453 Points

I tried changing the headers in both the index.html and contact.html files and I am still getting a 404 when trying to render both in workspace :(

Logan R
Logan R
22,989 Points

Did you change the html files that are in your root directory, or the files that are in the template directory? If you didn't change the template files, try changing those.

Dean Friedland
Dean Friedland
2,453 Points

I figured it out. If you look at both files both links (are referenced twice, not just once on line 56). For example, they need to be changed on lines 27 and 28 in index.html as well as 56 and 57. Same for contact.html.

Either way this should have been addressed by the Treehouse staff in a notation somewhere on the video page.

Thanks for the help Logan.

Oh and I changed it to '/contact' not 'contact'

Logan R
Logan R
22,989 Points

Glad you figured it out! I hope you don't run into any more trouble :) But if you do, feel free to ask on the forums.

Dean Friedland
Dean Friedland
2,453 Points

Thanks bud!!!

I am starting a 12 week LAMP stack dev bootcamp (www.lampcamp.guru) on Tuesday and part of my pre-work is to have attained 2000 points on the Treehouse PHP track. Every minute counts right now as I only have 1,450 so far. That is why I was upset about how long it was taking to figure out. :)

I get the same error.

Lee Ravenberg
Lee Ravenberg
10,018 Points

Logan R has posted the fix into a comment. Basically the html templates have anchor tags to contact.html and index.html, while in the $app->get() method you are listening for the / or /contact string in the adres bar. Not /index.html or /contact.html

Lee Ravenberg
Lee Ravenberg
10,018 Points

If you opt for the option to edit the template's anchor tags, you can point to homepage by using this code:

<a href="../slim">About</a>
``` while still having your $app->get() method listening for **/**.

Create a .htaccess file inside into your webroot and paste the following code. It should work.

RewriteEngine On

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]