Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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:

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

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.


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