将/index.php重定向到/不使用htaccess

时间:2018-07-02 09:06:34

标签: laravel

我想不使用.htaccess文件将http://localhost/mysite/index.php重定向到http://localhost/mysite

我已经写了<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <ul id="root"></ul> <button class="save">Save</button>并将其放在{ "personal-information":{ "child":{ "first_name":"first_name", "addresses":{ "child":{ "address1":"address1", "address2":"address2" } } } }, "general-information":{ "child":{ "allergies":"allergies", "habits":"habits" } } } 中,但这不起作用

Middleware

Chrome错误:

  

此页面无效   本地主机将您重定向了太多次。   尝试清除您的cookie。   ERR_TOO_MANY_REDIRECTS

3 个答案:

答案 0 :(得分:0)

在Routes.php或route / web.php中,只需定义一条路由即可

Route::get('/', function() { 
    return view('viewname');
}); 

或者如果您想保持其使用方式,请使用

return redirect()->intended('viewname');

https://laravel.com/docs/5.6/routing

答案 1 :(得分:0)

尝试一下:

if (strpos($request->fullUrl(), 'index.php') !== false) {
    $newUrl = str_replace('index.php', '', $request->fullUrl());
    return Redirect::to($newUrl, 301);
}

答案 2 :(得分:0)

您需要检查实际请求的路径,而不是URL。我现在尚未对其进行测试,但是我假设getUrl是通过路由构建的,并且默认情况下添加了index.php

那为什么不使用php:$_SERVER['REQUEST_URI']

public function handle($request, Closure $next)
{
    if(strpos($_SERVER['REQUEST_URI'], 'index.php'))
        return Redirect::to('/', 301);

    return $next($request);
}

因为您将收到真实的URI,而index.php可能是唯一的调用,您甚至可以不进行搜索就进行检查,这会更快:

public function handle($request, Closure $next)
{
    if($_SERVER['REQUEST_URI'] === "/index.php")
        return Redirect::to('/', 301);

    return $next($request);
}

但请记住:

.htaccess是迄今为止更好,更快的解决方案!您应该完全更改它的重写。您上面编写的代码将在您身边的每次调用中执行。如果您是出于SEO的原因而这样做,请记住,这些事情会增加您所有的加载时间!

注意:上面的代码未经测试。 ;)