Laravel 4 All Routes Except Home Result in 404 Error

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 isTasksController
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.
Answer
Solution:
Just for a laugh, see if/index.php/hello
works.
If so, then most likely it's a.htaccess
problem.
Answer
Solution:
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!
Answer
Solution:
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!
Answer
Solution:
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.
Answer
Solution:
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
Answer
Solution:
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.
Answer
Solution:
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>
Answer
Solution:
You don't need a / when defining anything other than home:
Route::get('hello', function()
{
return View::make('hello');
});
Should work.
Answer
Solution:
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>
Answer
Solution:
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
Answer
Solution:
There was a little flaw in a previous answer. This might help out.
$ sudo a2enmod rewrite
$ sudo service apache2 restart
Hope it does.
Answer
Solution:
I had this problem (on Windows with manually installed Apache 2.2), and the cause was a missingAllowOverride
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 useAllowOverride All
if this doesn't work and you don't care for security.
Answer
Solution:
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>
Answer
Solution:
For Nginx users, you probably copied the thedefault
virtualhost - but Laravel requires some customization on thelocation
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 runsudo service nginx reload
to make the changes come into effect.
Answer
Solution:
Go to
nano /etc/nginx/sites-available/yoursite
find try_file and replace as below:
try_files $uri $uri/ /index.php?$query_string;
Answer
Solution:
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 myDocumentRoot
:
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
Answer
Solution:
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
Answer
Solution:
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)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xmlhttprequest error flutter
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.