Laravel 5 Apache mod_rewrite 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31227667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 Apache mod_rewrite not working
提问by Jan Schuermann
Yes there, have been similar questions, but the suggested solutions are not working for me. So I've set up Laravel, and the installation works fine. Well, as long as I'm staying on the base route
是的,有类似的问题,但建议的解决方案对我不起作用。所以我已经设置了 Laravel,并且安装工作正常。好吧,只要我留在基本路线上
localhost/page/
as soon as I'm trying to hit one of my routes, let's say
一旦我试图到达我的路线之一,让我们说
localhost/page/subpage
and the route is configured to return a view
并且路由被配置为返回视图
Route::get('subpage', ['as' => 'subpage', 'uses' => 'GenericPageController@subpage']);
The method subpagein the Controller:
subpage控制器中的方法:
public function subpage()
{
return view('base.subpage');
}
I simply get a 404 response from the server whenever i try hitting one of the routes. It doesnt matter what controller I'm using, or if its returning a view or a closure.
每当我尝试点击其中一条路线时,我都会收到来自服务器的 404 响应。我正在使用什么控制器,或者它是否返回视图或闭包都无关紧要。
- mod_rewrite is enabled
- On the main directory
AllowOverrideis set toAll
- mod_rewrite 已启用
- 在主目录
AllowOverride上设置为All
my .htaccessin the page/publicfolder looks like the following:
我.htaccess的page/public文件夹,如下所示:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Anyone has an idea what the problem could be?
任何人都知道问题可能是什么?
回答by Mike Rockétt
The .htaccessfile won't load up because it is in the publicdirectory, which is really meant to be your document root. As such, you should be accessing the app by going to localhost/page/public/subpage.
该.htaccess文件不会加载,因为它位于public目录中,该目录实际上是您的文档根目录。因此,您应该通过转到 来访问该应用程序localhost/page/public/subpage。
If you want to use localhost/page/subpagewhilst keeping your directory structure intact, then you need to add a new .htaccessfile to the pagedirectory, and deletethe .htaccessfile from the publicdirectory.
如果要localhost/page/subpage在保持目录结构不变的情况下使用,则需要向目录中添加一个新.htaccess文件page,然后从目录中删除该.htaccess文件public。
/page/.htaccesscontents:
/page/.htaccess内容:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /page/
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]
# Send requests to public directory...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.php [L]
</IfModule>
Unfortunately, due to the way in which Laravel obtains the request information, you will need to group your routes to the pagedirectory in your route configuration:
不幸的是,由于 Laravel 获取请求信息的方式,您需要将路由分组到page路由配置中的目录:
Route::group(['prefix' => 'page'], function()
{
Route::get('subpage', ['as' => 'subpage', 'uses' => 'GenericPageController@subpage']);
// Your other routes ...
});
Essentially, and in my opinion, this is the easiest way to work around this. You could also move the contents of your publicdirectory up one level and change the paths accordingly in the bootstrap file.
从本质上讲,在我看来,这是解决此问题的最简单方法。您还可以将public目录的内容上移一层,并在引导文件中相应地更改路径。

