可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
Just for a laugh, see if /index.php/hello
works.
If so, then most likely it's a .htaccess
problem.
回答2:
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!
回答3:
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!
回答4:
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):
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
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.
回答5:
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.
回答6:
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
回答7:
You don't need a / when defining anything other than home:
Route::get('hello', function()
{
return View::make('hello');
});
Should work.
回答8:
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>
回答9:
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
回答10:
There was a little flaw in a previous answer.
This might help out.
$ sudo a2enmod rewrite
$ sudo service apache2 restart
Hope it does.
回答11:
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.
回答12:
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 sergey.donchenko@gmail.com
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>
回答13:
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>
回答14:
Go to
nano /etc/nginx/sites-available/yoursite
find try_file and replace as below:
try_files $uri $uri/ /index.php?$query_string;
回答15:
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.
回答16:
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
回答17:
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
回答18:
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)