今天发现了一个神奇的现象,在laravel框架下,网站的首页后面加上双斜杠(//),页面显示的尽然不是首页,是一个空白的页面状态码是200,或者报404错误,这个显然不是我们想要的效果。
我们期望的效果是如首页是www.chateach.com,那么'www.chateach.com/',或者是'www.chateach.com//',或者是''www.chateach.com///''都应该是显示首页。按道理双斜杠应该自动处理的,但后端laravel 对于双斜杆,尤其是域名后紧跟的双斜杆处理的方式很奇葩,解决方法也很简单,修改下入口文件就可以。laravel框架的入口文件在网站根目录的'index.php'。
laraver入口文件代码
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <taylor@laravel.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI']), PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
require_once __DIR__.'/public/index.php';
解决方案
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI']= '/'.ltrim($_SERVER['REQUEST_URI'],'/'), PHP_URL_PATH)
);