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 & Running Slim

Christiaan Quyn
Christiaan Quyn
14,706 Points

.htaccess & Apache configuration (httpd.conf) on MAMP

I just spent the entire day figuring out why I kept getting a 404 error on my URL's after installing the Slim framework and running it on my localhost. I didn't find any informative answers on the forum that really helped me, so I figured I'd share what I learnt :)

The Problem-

I set up a site with the Slim framework using Composer and create an index.php with the following example code form slimframework.com:

<?php
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});
$app->run();

Then I try to access the page using http://localhost/hello/bob. I get back a 404 Page Not Found page. I was able to get to access the page using http://localhost/index.php/hello/bob.

The Solution-

First in your Apache configuration file (httpd.conf) (mine was in C:/MAMP/conf) set your document root to your project file that contains the index.php and .htaccess files

Then looking at /vendor/slim/.htaccess (included w/ Composer Slim install) and the URL Rewriting section of the Slim framework documentation. I added a copy of the /vendor/slim/.htaccess file to the same folder as my index.php file. The contents of the file are:

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

Then accessed the page using http://localhost/hello/bob.

I went through a ton of resources online but I still don't really understand why it couldn't be as simple Hampton's Video and would greatly appreciate someone telling me what I just did.

Sources-

https://stackoverflow.com/questions/9747640/slim-framework-always-return-404-error https://stackoverflow.com/questions/34844292/mamp-slim-php-still-have-404-error