laravel 路由如何写,才能实现 /post 和 /post/ 指向一个位置?
现在,/post/总会跳到别的页面,而不是跳到/post的位置。
我的laravel版本是4.2,下面是测试代码。
php
Route::get('post', function() { return 'w/out slash'; }); Route::get('post/', function() { return 'with slash'; });
php
Route::get('post/', function() { return 'with slash'; }); Route::get('post', function() { return 'w/out slash'; });
以上两种配置,结果都如下,都是/post/ 301 跳到别的页面,/post页面正常。
curl http://localhost/gitcafe/public/post
with slash
curl http://localhost/gitcafe/public/post/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://localhost/post">here</a>.</p>
</body></html>
laravel php-laravel php laravel-5 laravel5
崔斯特REX
9 years, 7 months ago
Answers
会吗?L5
Route
组件的行为我记得是:
- 无论带结尾斜线与否,均视为同一个路由;
-
带斜线的301跳转到不带斜线的(至少对
GET
如此); - 同一个路由指定两次时,后边的覆盖前边的。
以下是测试用例与结果:
Case 1
$ diff my_l5_app/app/Http/routes.php l5_fresh/app/Http/routes.php
22,24d21
<
< Route::get('post/', function() { return 'with slash'; });
< Route::get('post', function() { return 'w/out slash'; });
$ curl http://test1.l5.invalid/post/
这里Web server提示了301跳转到 http://test1.l5.invalid/post
SF的前台渲染有误,结果见 http://pastebin.com/ZMh0smvq
$ curl http://test1.l5.invalid/post
w/out slash
Case 2
$ diff my_l5_app/app/Http/routes.php l5_fresh/app/Http/routes.php
22,24d21
<
< Route::get('post', function() { return 'w/out slash'; });
< Route::get('post/', function() { return 'with slash'; });
$ curl http://test1.l5.invalid/post/
这里Web server提示了301跳转到 http://test1.l5.invalid/post
SF的前台渲染有误,结果见 http://pastebin.com/ZMh0smvq
$ curl http://test1.l5.invalid/post
with slash
月宫大魔王
answered 9 years, 7 months ago
大大小冰块
answered 9 years, 7 months ago