[Solved] Routes not working in Laravel 4 (MAMP)

I am using MAMP to develop a Laravel 4 application, i.e. using a Mac. If you are developing in Windows, you need to make slight variations here and there.

Background:
You have a Laravel 4 application called my_app running on MAMP. Everything works fine, i.e. you see the welcome page when you visit localhost:8888/my_app/public/ (I am using port 8888, you may have 80). You have defined a route in your routes.php file as follows:

Route::get('signin', function()
{
return View::make('signin');
});

Explanation: When you visit localhost:8888/my_app/public/signin/ you expect to reach the view called ‘signin’. For this tutorial, I am going to use this route only.

The Problem:
When you visit localhost:8888/my_app/public/signin/ you get a 404 Page Not Found error. After numerous attempts and spending about 6 hours this afternoon, I was able solve this problem. I picked up scraps and bits from here and there (mainly Stackoverflow) and here is the complete solution to save you some time.
=============================

The Solution:
I will assume that your application is located in /Applications/MAMP/htdocs/.

Step 1.
Check if your desired route (‘signin’ in this case) is working when you visit localhost:8888/my_app/public/index.php/signin/, If it works, then continue to the next point. If it doesn’t, then unfortunately the next steps won’t resolve your problem.

 

Step 2.
You should have a .htaccess file in your /public folder. If it is a fresh installation of Laravel, replace the content with the following:

The .htaccess file in /public should look like this.

The .htaccess file in /public should look like this.

 

 

 

 

 

 

 

 

 

 

 

Copy the above code from here: https://github.com/laravel/laravel/blob/master/public/.htaccess

 

Step 3.
Create a new virtual host following the steps below:

Step 3A.
Go to Terminal and type sudo nano -w /private/etc/hosts. It is very important to type sudo. If you are having issues with this file being locked, make sure you are the owner of this file and you have Read and Write access on the file. [Help]

Step 3B.
Keep pressing Ctrl + V until you reach the end of the file. Then enter a new line: 127.0.0.1 my_app.dev. If this tutorial works for you, you will be able to reach your application by typing http://my_app.dev:8888/public/. [This may not be the most elegant way to do it. I am learning Laravel 4 at this time.]

So your hosts file should now look like:

'##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.1 my_app.dev

Step 3C.
Enter Ctrl + O (O, not zero) -> Press ‘Enter’ -> Press Ctrl + x to exit nano.

Step 3D.
Setting up virtual host

  • At the Terminal, type: nano -w /Applications/MAMP/conf/apache/vhosts.conf
  • Paste the following text in nano:<VirtualHost *:8888>
    ServerName my_app.dev
    DocumentRoot /Applications/MAMP/htdocs/path_to_my_app/
    <Directory /Applications/MAMP/htdocs/path_to_my_app/>
    <IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    AllowOverride All
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    </IfModule>
    </Directory>
    </VirtualHost>
    IMPORTANT: Change the parts in bold. Also, I am not specifying the path to the public folder.
  • Enter Ctrl + O (O, not zero) -> Press ‘Enter’ -> Press Ctrl + x to exit nano.
  • Enable Virtual Hosts in your MAMP httpd.conf file.
    –> At the terminal type: nano -w /Applications/MAMP/conf/apache/httpd.conf–> Keep pressing Ctrl + V until you reach the end of the file.–> Enter the two lines:
    NameVirtualHost *:8888
    Include /Applications/MAMP/conf/apache/vhosts.conf
    –> IMPORTANT: Also make sure that the following line in httpd.conf is uncommented, i.e. no ‘#’ at the beginning.
    LoadModule rewrite_module modules/mod_rewrite.so.

    –> Enter Ctrl + O (O, not zero) -> Press ‘Enter’ -> Press Ctrl + x to exit nano.

Step 4.
Restart Apache by typing /Applications/MAMP/bin/apache2/bin/apachectl restart at the Terminal.

Step 5.
Restart MAMP and try visiting: http://my_app.dev:8888/public/signin/.

Please let me know if this worked for you. Comments and feedback are always appreciated.

Also, if you’re stuck, you can read this really well-written tutorial on how to create virtual host for MAMP.

 


Update – 28 May, 2014

I just realized that in step Step 3D above, you can also specify the path to your public folder.

So your vhosts.conf file will look like:

<VirtualHost *:8888>
ServerName my_app.dev
DocumentRoot /Applications/MAMP/htdocs/path_to_my_app/public//
<Directory /Applications/MAMP/htdocs/path_to_my_app/public/>
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
AllowOverride All
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
</Directory>
</VirtualHost>

Making this change will allow you to visit your home page by visiting the URL http://my_app.dev:8888, i.e. no need for the public. This is how your website should behave anyways.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.