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 subpage
in 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
AllowOverride
is set toAll
- mod_rewrite 已启用
- 在主目录
AllowOverride
上设置为All
my .htaccess
in the page/public
folder 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 .htaccess
file won't load up because it is in the public
directory, 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/subpage
whilst keeping your directory structure intact, then you need to add a new .htaccess
file to the page
directory, and deletethe .htaccess
file from the public
directory.
如果要localhost/page/subpage
在保持目录结构不变的情况下使用,则需要向目录中添加一个新.htaccess
文件page
,然后从目录中删除该.htaccess
文件public
。
/page/.htaccess
contents:
/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 page
directory 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 public
directory up one level and change the paths accordingly in the bootstrap file.
从本质上讲,在我看来,这是解决此问题的最简单方法。您还可以将public
目录的内容上移一层,并在引导文件中相应地更改路径。