dongzhui9936 2016-08-27 06:42
浏览 184
已采纳

在Apache服务器上找不到Laravel 5.1 404

I have currently hosted laravel 5.1 application to my 1and1.com server and I am facing a 404 not found errors to all my routes except for the root route i.e www.example.com/server/

The root route is working with get request but all the other routes are coming as 404 not found , everything is working good in localhost.

My application folder structure:

  • -example.com

    • -server
      • App
      • database
      • config
      • storage
      • .env
      • .htaccess
      • index.php
      • server.php

as you can see that I have removed the default laravel public folder and moved the contents to root.

My .htaccess

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]   RewriteCond     %{REQUEST_URI} !^  RewriteRule ^(.*)$ /$1 [L]  </IfModule>

My Index.php

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylorotwell@gmail.com>
 */

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels nice to relax.
|
*/

require __DIR__.'/bootstrap/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

Server.php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylorotwell@gmail.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__.''.$uri)) {
    return false;
}

require_once __DIR__.'index.php';

Routes.php

Route::get('/', function(){
        echo'test';exit();
    });//test

Route::get('/s', function(){
        echo'testss';exit();
    });//test   

now if you see that the first route is working fine, but when I do www.example.com/server/s this gives 404 not found.

  • 写回答

3条回答 默认 最新

  • doulai8405 2016-08-27 09:12
    关注

    Whenever you host your laravel application on a shared server, you need to separate the public folder.

    All the contents of public folder should go to public_html folder of the server, while all the other folders need to go on root folder..

    So, to get you going, first make a directory called laravel-app in your server root. Inside this folder, upload all the folders and files but do not upload the public folder.

    Once all the files are uploaded, upload the contents of public folder to public_html folder.

    Now, you need to tell laravel that you have changed the default structure of the folders. In order to do that, open index.php file which is located inside public_html folder.

    Replace this line:

    require __DIR__.'/bootstrap/autoload.php';
    
    $app = require_once __DIR__.'/bootstrap/app.php';
    

    with this line:

    require __DIR__.'/../laravel-app/bootstrap/autoload.php';
    
    $app = require_once __DIR__.'/../laravel-app/bootstrap/app.php';
    

    Now, re-upload the index.php file, and you should get the desired output.

    Hope this helps you out. Happy Coding. Cheers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵