【问题标题】:Laravel route strange behaviorLaravel 路由奇怪的行为
【发布时间】:2014-10-23 11:49:32
【问题描述】:

以下代码

Route::get('test/{n}', function ($n) {
    return $n;
});

App::missing(function () {
    return Response::make("Dude, that's a 404 !", 404);
});

适用于任何类似的东西

http://localhost/myproject/public/test/something

但是

http://localhost/myproject/public/test/

重定向到

http://localhost/test

我认为应该改为显示

Dude, that's a 404 !

我可能错误搜索了 SO 论坛,但是有人对这种奇怪的行为有解释吗?

编辑:@Mohammad Walid

好的,所以,我将 routes.php 更改为:

Route::get('/', 'HomeController@showWelcome');

Route::get('test', function () {
    return 'test';
});

App::missing(function () {
    return Response::make("Dude, that's a 404 !", 404);
});

没有删除你在public/.htaccess中提到的那行。

这行得通:

base_url/test

但这没有(重定向到localhost/test):

base_url/test/

怎么可能,因为你说删除线会删除该功能?

EIDT 2:

由于事实证明问题来自 MAMP 没有正确解释尾部斜杠重定向条件,因此在 MAMP 错误库中创建了一个错误报告:

http://bugs.mamp.info/view.php?id=4586

【问题讨论】:

  • 请检查更新

标签: laravel parameters routing


【解决方案1】:

这是因为 Laravel 默认重定向尾部斜杠。

app/public/.htaccess 文件中,您可以找到以下内容:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301] #This line causes the problem

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

改成:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

看看trailing slashes discussion

注意如果您删除前一行,您的应用程序将无法处理尾部斜杠。 前任。如果你有路线

Route::get('test', function() {
    return 'test';
}

假设已请求 base_url/test/ 而不是 base_url/test,服务器将以 404 状态响应。

更新

这似乎是一个只发生在本地开发中的问题(我使用 XAMPP)。

该规则的预期结果如下:

.htaccess
RewriteRule ^(.*)/$ /$1 [L,R=301]

Input (request)
http://localhost/website/test/

Output (after redirect)
http://localhost/website/test

但我们在本地开发中实际拥有的东西:

.htaccess
RewriteRule ^(.*)/$ /$1 [L,R=301]

Input (request)
http://localhost/website/test/

Output (after redirect)
http://localhost/test #this is wrong, according to the rule above it should redirects to http://localhost/website/test

那么,如果您删除规则,您将拥有什么:

Request
http://localhost/website/test

Response
test


Request
http://localhost/website/test/

Response
Status 404
Dude, that's a 404 !

The expected response:
test

这就是我的意思

您的应用程序将无法处理尾部斜杠。

服务器将响应 404 状态。

我已经使用online htaccess tester 测试了该规则并且它按预期工作,这就是为什么我认为问题只存在于本地开发中。

【讨论】:

  • @user3856210 不客气,如果可行,您可以接受该解决方案。
  • 对不起,我发的太快了,我正在编辑它,因为我完全理解了你的回复,我正在尝试写一个评论,我会解释我不明白的地方(如果这使得对你有感觉)。我会在我完全理解后立即将其标记为已解决,给我五分钟的时间来表达另一条评论并更好地解释我的误解:)
  • @user3856210 当然,慢慢来。
  • 哇,没想到会有更好的答案:D 非常感谢!如果我能给你两个而不是一个,我会的!再次感谢您花时间回答我的问题 :) PS:我刚刚在在线测试仪上进行了测试,使用了整个 .htaccess,正如预期的那样,它可以工作:)
  • @user3856210 很高兴这对你有帮助:)
猜你喜欢
  • 1970-01-01
  • 2017-09-01
  • 2016-09-25
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 2016-11-28
相关资源
最近更新 更多