最近在使用php laravel框架的时候 在routes.php文件中添加路由,老是出现404错误,如图
使用跟目录可以访问 Route::get('/', 'HomeController@showWelcome');
如果非跟目录就会出现404 页面找不到的错误,如下
Route::get('user', 'UserController@index');
解决方法:
1.在apache conf开启rewrite模块,把下面这句话前面的#去掉。
LoadModule rewrite_module modules/mod_rewrite.so
2.在conf文件中找到directory 把AllowOverride None 改成 AllowOverride All
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
3.在laravel项目工程的public目录下添加.htaccess文件 ,文件内容如下
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
然后重新apache服务重新访问就可以运行了。