Redirect /index.php to / without htaccess


Redirect /index.php to / without htaccess



I want redirect http://localhost/mysite/index.php to http://localhost/mysite without using .htaccess file



I've written a Middleware and put it in Kernel.php but this does not work


Middleware


Kernel.php


public function handle($request, Closure $next)
{
if(strpos($request->url(), 'index.php'))
return Redirect::to('/', 301);

return $next($request);
}



Chrome error:


This page isn’t working
localhost redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS





this is one of the most stupid things I have seen lately.
– Leo instanceof Kelmendi
4 hours ago





@LeoinstanceofKelmendi Why?! this is essential for me. i also redirect https to http with if ($request->secure()) return Redirect::to($request->path(), 301); and this work well.
– dev4xy
4 hours ago


if ($request->secure()) return Redirect::to($request->path(), 301);





you want when you access this http://localhost/mysite/index.php it should redirect localhost/mysite , so you want restrict public/index.php file ??
– Hamelraj
2 hours ago


http://localhost/mysite/index.php


public/index.php




2 Answers
2



Try this:


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



In Routes.php or routes/web.php just define a Route with


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



or if you want to keep it the way you used it, use



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


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



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





No, i want redirect /index.php to / , now both are load a same view. i want only / url.
– dev4xy
3 hours ago



/index.php


/


/





I must admit that I dont get why you want it without htaccess. Laravel's default config handles this issue clearly for you. Could you tell us what exactly you want to do?
– demo
3 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift