laravel url重定向到根文件夹后的Laravel斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22063520/
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 slash after url redirects to root folder
提问by user1507868
I'm new to Laravel and it seems a great framework but i have a question when it comes to routing. I want to route all get requests to one controller action named PageController@view but when the prefix is admin i want it to route to Admin\AdminController@view. I have the following code:
我是 Laravel 的新手,它似乎是一个很棒的框架,但是在路由方面我有一个问题。我想将所有 get 请求路由到一个名为 PageController@view 的控制器操作,但是当前缀为 admin 时,我希望它路由到 Admin\AdminController@view。我有以下代码:
Route::get('/{slug?}', 'PageController@view');
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin', 'before' => 'auth'), function()
{
Route::get('/', 'DashboardController@main');
});
But when i go to: /admin, Laravel treats it like a slug instead of a prefix. Is there somekind of except function for the first line or do i simply have to switch the above lines to get the right result?
但是当我转到:/admin 时,Laravel 将其视为 slug 而不是前缀。第一行是否有某种异常功能,或者我是否只需切换上面的行以获得正确的结果?
New problem:
新问题:
But now i have a new problem when i go to http://www.mysite.com/laravel/public/admin/, Laravel redirects me to http://www.mysite.com/admin/instead of calling AdminController@main. When i remove the slash at the end like this: http://www.mysite.com/laravel/public/adminit works fine.
但是现在我在访问http://www.mysite.com/laravel/public/admin/时遇到了一个新问题,Laravel 将我重定向到http://www.mysite.com/admin/而不是调用 AdminController@main . 当我像这样删除末尾的斜杠时:http: //www.mysite.com/laravel/public/admin它工作正常。
回答by alexrussell
If you specify those routes in the opposite order it should work. Do your most specific routes first, and the more generic ones later. Think about it as a "catch this, this, and this, and then fall back to this generic one when none of the above match".
如果您以相反的顺序指定这些路线,它应该可以工作。先做最具体的路线,然后再做更通用的路线。把它想象成“抓住这个、这个和这个,然后当以上都不匹配时回退到这个通用的”。
Your new problem lies in the (bad, IMO) redirect rule that Laravel 4.1 ships with. The rules very much assume that your Laravel public
directory sits on the document root of the server. Here's the rule:
您的新问题在于 Laravel 4.1 附带的 (bad, IMO) 重定向规则。规则非常假设您的 Laravelpublic
目录位于服务器的文档根目录。这是规则:
RewriteRule ^(.*)/$ / [L,R=301
As you can see, that's saying "if the current URL ends in a slash, just redirect it to /{url}", thus getting rid of the slash. This is all well and good, just as long as your installation sits on your server's document root.
如您所见,这是说“如果当前 URL 以斜杠结尾,只需将其重定向到 /{url}”,从而摆脱斜杠。这一切都很好,只要您的安装位于服务器的文档根目录中即可。
You can fix this by specifying a RewriteBase
(in your case RewriteBase /laravel/public
) above that rule in your public/.htaccess
file, but of course, this will now change from environment to environment. This is why i consider the changing of Laravel 4.0's RedirectIftrailingSlash
functionality to be a .htaccess
rule to be a bad idea.
您可以通过在文件中指定该规则上方的RewriteBase
(在您的情况下RewriteBase /laravel/public
)来解决此问题public/.htaccess
,但当然,现在这将因环境而异。这就是为什么我认为改变 Laravel 4.0 的RedirectIftrailingSlash
功能.htaccess
是一个坏主意的规则。
Alternatively, you can remove that RewriteRule
entirely and try not to worry about the fact that your app will now respond on both URLs /laravel/public/admin
and /laravel/public/admin/
.
或者,您可以RewriteRule
完全删除它,并尽量不要担心您的应用程序现在会同时响应 URL/laravel/public/admin
和/laravel/public/admin/
.
回答by Lazirro
Check if you have folder "admin"in your publicfolder. Laravel automaticaly redirects if there is one.
检查您的公共文件夹中是否有文件夹“admin”。如果有,Laravel 会自动重定向。
回答by Andreyco
Since the order matters, try this
由于顺序很重要,试试这个
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin', 'before' => 'auth'), function()
{
Route::get('/', 'DashboardController@main');
});
Route::get('/{slug?}', 'PageController@view');
Redirection problemLaravel redirects from URL with trailing slash to URL without trailing slash - default behaviour. (If you enter URL without trailing slash -> no redirect needed) Actual redirection is not the problem. The problem is, Laravel cannot guess right "base" URL and generates wrong redirect URL for you.
重定向问题Laravel 从带有尾部斜杠的 URL 重定向到没有尾部斜杠的 URL - 默认行为。(如果您输入的 URL 不带斜杠 -> 不需要重定向)实际重定向不是问题。问题是,Laravel 无法猜测正确的“基本”URL 并为您生成错误的重定向 URL。
Open up app/config/app.php
and set url
entry to match root of your project. I guess it is http://www.mysite.com/laravel/public
in your case.
打开app/config/app.php
并设置url
条目以匹配项目的根目录。我想这是http://www.mysite.com/laravel/public
你的情况。
回答by Sahil Ahuja
The following RewriteRule works for me. I had to remove the / in /$1 from alexrussel's answer above. Including the / redirects me to root folder.
以下 RewriteRule 对我有用。我不得不从上面 alexrussel 的回答中删除 / 中的 /$1。包括 / 将我重定向到根文件夹。
#For http://localhost/work/proj1/public/a/ RewriteBase is /work/proj1/public
RewriteBase /repo/qs/public
RewriteRule ^(.*)/$ [L,R=301]
I am using laravel v4.2, apache v2.4.7, php v5.5.9 on Linux Mint 17 64-bit.
我在 Linux Mint 17 64 位上使用 laravel v4.2、apache v2.4.7、php v5.5.9。
回答by user5023913
If you know the directory name, make a rule for it. This works fine for me:
如果您知道目录名称,请为其制定规则。这对我来说很好用:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteRule ^(forum)($|/) - [L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>