[英]Laravel 5.1 404 not found on apache server
我目前已將laravel 5.1應用程序托管到我的1and1.com服務器上,並且我面臨除根路由(即www.example.com/server/)以外的所有所有路由的404 not found錯誤
根路由正在與get請求一起使用,但是所有其他路由都以404 not found的形式出現,在localhost中一切正常。
我的應用程序文件夾結構:
-example.com
如您所見,我已經刪除了默認的laravel公用文件夾,並將內容移至了root。
我的.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>
我的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
現在,如果您看到第一條路由工作正常,但是當我執行www.example.com/server/s時,則找不到404。
如果您的項目遷移到了新服務器,則可能會出現這種情況,如果您使用默認的項目文檔根配置,則默認情況下可以禁用它來讀取.htaccess文件。 您需要在/etc/httpd/conf/httpd.conf
中將AllowOverride選項從“ None”更改為“ All”(例如)(如果使用centos的話):
<Directory /var/www/html/public>
...
AllowOverride All
...
</Directory>
然后重新加載服務器以應用更改
sudo service httpd reload
除了Alex的答案,如果您使用的是apache ,請嘗試
sudo a2enmod rewrite
並重新加載Apache
sudo service apache2 reload
希望這可以幫助!
每當將laravel應用程序托管在共享服務器上時,都需要分離公用文件夾。
公用文件夾的所有內容都應轉到服務器的public_html
文件夾,而所有其他文件夾都應位於根文件夾上。
因此,要開始使用,請首先在服務器根目錄中創建一個名為laravel-app
的目錄。 在此文件夾內,上傳所有文件夾和文件,但不上傳public folder
。
上傳完所有文件后,將public
文件夾的內容上傳到public_html
文件夾。
現在,您需要告訴laravel您已經更改了文件夾的默認結構。 為此,請打開位於public_html
文件夾內的index.php
文件。
替換此行:
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
用這一行:
require __DIR__.'/../laravel-app/bootstrap/autoload.php';
$app = require_once __DIR__.'/../laravel-app/bootstrap/app.php';
現在,重新上傳index.php文件,您應該獲得所需的輸出。
希望這可以幫助你。 編碼愉快。 干杯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.