Laravel 4 All Routes Except Home Result in 404 Error
LaravelLaravel 4RoutingHttp Status-Code-404Laravel RoutingLaravel Problem Overview
I installed Laravel 4 using Composer and also set up a virtual host. Currently, only the root route is working:
<?php
Route::get('/', function()
{
return View::make('hello');
});
This is not:
Route::get('/hello', function()
{
return View::make('hello');
});
What I'm trying to hit is TasksController
at /tasks
:
Route::resource('tasks', 'TasksController');
This is giving me 404 error as well. What could I be doing wrong? I have a default .htaccess file at the root of my project:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I am using localhost on a Mac.
Laravel Solutions
Solution 1 - Laravel
Just for a laugh, see if /index.php/hello
works.
If so, then most likely it's a .htaccess
problem.
Solution 2 - Laravel
Had the same problem running Laravel 4 on WAMP (Windows 8).
The solution that worked for me was:
-
Open apache httpd.conf and find this line :
#LoadModule rewrite_module modules/mod_rewrite.so
-
Uncomment this line (remove the
#
) -
Save
httpd.conf
-
Restart WAMP
It should be working!
Solution 3 - Laravel
I had the same problem and the solution was enable the rewrite mod on Apache2
In a terminal use the following commands:
$ sudo a2enmod rewrite
$ sudo service apache2 restart
And magic!
Solution 4 - Laravel
Had exactly the same problem.
Two things I needed to do to fix this:
-
enable rewrite_module in Apache
-
Change the AllowOverride from None to All, example (Apache 2.4.9):
<Directory "c:/www"> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted </Directory>
FYI:
use Laravel Homestead together with VirtualBox and Vagrant instead of WAMP. It contains Nginx instead of Apache but everything works by default as it is configured for Laravel explicitly.
Solution 5 - Laravel
FOR UBUNTU USERS - tested for Ubuntu 18.04
1- Enable mod rewrite
sudo a2enmod rewrite
2- Change AllowOverride in apache conf file:
sudo nano /etc/apache2/apache2.conf
Change the AllowOverride from None to All in this block
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
3- Restart Apache
sudo service apache2 restart
Solution 6 - Laravel
Even after enabling mod_rewrite, you may face this problem if you are aliasing your laravel/public folder.
Adding
RewriteBase /your_apache_alias
to .htaccess does the trick.
Solution 7 - Laravel
I tried all these with no success then i found another post and changed my .htaccess to this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L]
</IfModule>
Solution 8 - Laravel
You don't need a / when defining anything other than home:
Route::get('hello', function()
{
return View::make('hello');
});
Should work.
Solution 9 - Laravel
It's like @GaryJ & @MartinGomez said. This is the content of the .htaccess that should be set on the public folder for all your laravel 4 projects running on Apache server:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Solution 10 - Laravel
Since Laravel 4 is autoloading files from a map in a static file, you need to update that file when you add a new controller. Run this command to rebuild the static file:
php composer.phar dump-autoload
Solution 11 - Laravel
There was a little flaw in a previous answer. This might help out.
$ sudo a2enmod rewrite
$ sudo service apache2 restart
Hope it does.
Solution 12 - Laravel
I had this problem (on Windows with manually installed Apache 2.2), and the cause was a missing AllowOverride
in my VirtualHost as the root directory in httpd.conf was defaulted to None.
FileInfo Options=MultiViews
is the minimal set you need for Laravel's .htaccess
e.g.
<VirtualHost *:80>
DocumentRoot "C:/htdocs/domain.com/laravel/public"
ServerName foo.domain.com
<Directory "C:/htdocs/domain.com/laravel/public">
AllowOverride FileInfo Options=MultiViews
</Directory>
</VirtualHost>
Or use AllowOverride All
if this doesn't work and you don't care for security.
Solution 13 - Laravel
It's pretty clear that the problem appears because of the "mod_rewrite", in some cases just enabling this module in Apache is enough to get it fixed.
However, in my case, I had to extend the configuration for the VirtualHost in the following way:
<VirtualHost *:80>
ServerName adplus.local
ServerAdmin [email protected]
DocumentRoot /var/www/adplus.local/project/public
**<Directory "/var/www/adplus.local/project/public">
AllowOverride All
</Directory>**
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Solution 14 - Laravel
For Nginx users, you probably copied the the default
virtualhost - but Laravel requires some customization on the location
directive to allow the Laravel application to do the routing and not Nginx.
In your /etc/nginx/sites-available/your-site-name
replace the location directive with this:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
Then run sudo service nginx reload
to make the changes come into effect.
Solution 15 - Laravel
Go to
> nano /etc/nginx/sites-available/yoursite
find try_file and replace as below:
try_files $uri $uri/ /index.php?$query_string;
Solution 16 - Laravel
I had a similar problem on Ubuntu 14.04 with Lumen 5.4.
The solution was two parts for me. First in my vhost file /etc/apache2/sites-enabled/my_vhost.conf
, I had to add the following <Directory>
entry to allow overrides, after declaring my DocumentRoot
:
DocumentRoot /var/www/path/to/my/app/public
<Directory "/var/www/path/to/my/app/public">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Next, I had to enable rewrites with:
sudo a2enmod rewrite
sudo service apache2 restart
Solution 17 - Laravel
This update worked for me. In the .htaccess file simply add the following after the Rewriterules are turned on.
Options -Indexes
Options +FollowSymLinks
Worked like charm
Solution 18 - Laravel
If using IIS as web server, create a file with this name web.config on public folder or where index.php is with this configuration :
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
If you using apache edit .htaccess file and add this part
<Directory />
AllowOverride All
</Directory>
In linux and nginx there isn't this problem
Solution 19 - Laravel
This comment may be a little late but it may help. I had the same problem when routing to another view like this:
Route::get('index', function () {
return view('index');
});
Route::get('login', function () {
return view('login');
});
But didn't work so I tried everything i found in all the related posts but wasn't enough to solve my problem so i found this lines on the httpd.conf:
<Files ".ht*">
Require all denied
</Files>
So i changed "denied" to "granted" and also commented this line on my .htaccess:
#RewriteBase /
And worked!! I think this lines make Apache not consider the .htaccess file of your project so turning it to granted made the difference. Hope this can help someone with the same problem.
Working on Apache Server 2 @ Manjaro Linux (based on Archlinux)