Laravel 5 – Remove Public from URL

后端 未结 30 2593
甜味超标
甜味超标 2020-11-22 03:20

I know this is a very popular question but I haven\'t been able to find a working solution for Laravel 5. I\'ve been trying to migrate from Codeigniter for a long time, but

30条回答
  •  深忆病人
    2020-11-22 03:40

    Problem is if you type /public and it will still be available in the url, therefor i created a fix which should be placed in the public/index.php

      $uri = urldecode(
         parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
      );
    
      if(stristr($uri, '/public/') == TRUE) {
    
        if(file_exists(__DIR__.'/public'.$uri)){
    
        }else{
    
          $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
          $actual_link = str_replace('public/', '',$actual_link);
          header("HTTP/1.0 404 Not Found");
          header("Location: ".$actual_link."");
          exit();
          return false;
       }}
    

    this peace of code will remove public from the url and will give a 404 and then redirects to the url without the public

提交回复
热议问题