Laravel 5.1 404 在 apache 服务器上找不到
Laravel 5.1 404 not found on apache server
我目前已将 laravel 5.1 应用程序托管到我的 1and1.com 服务器,但我的所有路由都面临 404 未找到错误,但根路由除外,即 www.example。com/server/
根路由正在处理 get 请求,但所有其他路由都以 404 not found 的形式出现,在本地主机中一切正常。
我的应用文件夹结构:
-example.com
- -服务器
- 应用程序
- 数据库
- 配置
- 存储
- .env
- .htaccess
- index.php
- server.php
如您所见,我已删除默认 laravel public 文件夹并将内容移至根目录。
我的.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 ^(.*)/$ / [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 ^(.*)$ / [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 未找到。
无论何时在共享服务器上托管 laravel 应用程序,都需要分离 public 文件夹。
public 文件夹的所有内容应该放在服务器的 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 文件,您应该会得到想要的输出。
希望这对您有所帮助。快乐编码。干杯。
如果您的项目迁移到新服务器,则可能是因为您使用默认项目文档根配置,因此默认情况下可能会禁用它来读取您的 .htaccess 文件。您需要将 "None" 中的 AllowOverride 选项更改为 "All"(例如)/etc/httpd/conf/httpd.conf
中的选项值(如果是 centos):
<Directory /var/www/html/public>
...
AllowOverride All
...
</Directory>
然后重新加载您的服务器以应用更改
sudo service httpd reload
除了 Alex 的回答之外,如果您是 运行 apache,请尝试
sudo a2enmod rewrite
并重新加载 apache
sudo service apache2 reload
希望对您有所帮助!
我目前已将 laravel 5.1 应用程序托管到我的 1and1.com 服务器,但我的所有路由都面临 404 未找到错误,但根路由除外,即 www.example。com/server/
根路由正在处理 get 请求,但所有其他路由都以 404 not found 的形式出现,在本地主机中一切正常。
我的应用文件夹结构:
-example.com
- -服务器
- 应用程序
- 数据库
- 配置
- 存储
- .env
- .htaccess
- index.php
- server.php
- -服务器
如您所见,我已删除默认 laravel public 文件夹并将内容移至根目录。
我的.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 ^(.*)/$ / [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 ^(.*)$ / [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 未找到。
无论何时在共享服务器上托管 laravel 应用程序,都需要分离 public 文件夹。
public 文件夹的所有内容应该放在服务器的 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 文件,您应该会得到想要的输出。
希望这对您有所帮助。快乐编码。干杯。
如果您的项目迁移到新服务器,则可能是因为您使用默认项目文档根配置,因此默认情况下可能会禁用它来读取您的 .htaccess 文件。您需要将 "None" 中的 AllowOverride 选项更改为 "All"(例如)/etc/httpd/conf/httpd.conf
中的选项值(如果是 centos):
<Directory /var/www/html/public>
...
AllowOverride All
...
</Directory>
然后重新加载您的服务器以应用更改
sudo service httpd reload
除了 Alex 的回答之外,如果您是 运行 apache,请尝试
sudo a2enmod rewrite
并重新加载 apache
sudo service apache2 reload
希望对您有所帮助!