Slim框架API部署后出现404 NotFound错误求助
Slim API部署后404 NotFound问题排查
我正在开发毕业设计的Android应用,本地开发流程完全顺畅,但通过Filezilla将API文件上传至服务器后,持续出现404 NotFound错误。尝试修改.htaccess文件中的RewriteBase配置后问题仍未解决,不确定是否存在配置错误。相关代码及错误信息如下:
public/index.php
<?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Factory\AppFactory; require '../vendor/autoload.php'; require '../includes/DbOperations.php'; $app = AppFactory::create(); $app->setBasePath("/mylife/public"); $app->addErrorMiddleware(true, true, true); // My routes // ... $app->run();
.htaccess文件配置
我在public文件夹内外各放置了一个.htaccess文件:
外层(public文件夹外).htaccess
Options All -Indexes <Files .htaccess> order allow,deny deny from all </Files> <IfModule mod_rewrite.c> # Redirect to the public folder RewriteEngine On RewriteBase /mylife/ RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L] # Redirect to HTTPS # RewriteEngine On # RewriteCond %{HTTPS} off # RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </IfModule>
内层(public文件夹内).htaccess
Options All -Indexes <Files .htaccess> order allow,deny deny from all </Files> <IfModule mod_rewrite.c> RewriteEngine On # Redirect to HTTPS # RewriteEngine On # RewriteCond %{HTTPS} off # RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Some hosts may require you to use the `RewriteBase` directive. # Determine the RewriteBase automatically and set it as environment variable. # If you are using Apache aliases to do mass virtual hosting or installed the # project in a subdirectory, the base path will be prepended to allow proper # resolution of the index.php file and to redirect to the correct URI. It will # work in environments without path prefix as well, providing a safe, one-size # fits all solution. But as you do not need it in this case, you can comment # the following 2 lines to eliminate the overhead. RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$ RewriteRule ^(.*) - [E=BASE:%1] # If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the # absolute physical path to the directory that contains this htaccess file. # RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [QSA,L] </IfModule>
错误日志
[05-Nov-2023 02:05:24 Asia/Kuala_Lumpur] 404 Not Found Type: Slim\Exception\HttpNotFoundException Code: 404 Message: Not found. File: /home/sabrisae/public_html/ruangprojek/sadiahcafe/mylife/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php Line: 76 Trace: #0 /home/sabrisae/public_html/ruangprojek/sadiahcafe/mylife/vendor/slim/slim/Slim/Routing/RouteRunner.php(56): Slim\Middleware\RoutingMiddleware->performRouting() #1 /home/sabrisae/public_html/ruangprojek/sadiahcafe/mylife/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(76): Slim\Routing\RouteRunner->handle() #2 /home/sabrisae/public_html/ruangprojek/sadiahcafe/mylife/vendor/slim/slim/Slim/MiddlewareDispatcher.php(121): Slim\Middleware\ErrorMiddleware->process() #3 /home/sabrisae/public_html/ruangprojek/sadiahcafe/mylife/vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): Psr\Http\Server\RequestHandlerInterface@anonymous->handle() #4 /home/sabrisae/public_html/ruangprojek/sadiahcafe/mylife/vendor/slim/slim/Slim/App.php(199): Slim\MiddlewareDispatcher->handle() #5 /home/sabrisae/public_html/ruangprojek/sadiahcafe/mylife/vendor/slim/slim/Slim/App.php(183): Slim\App->handle() #6 /home/sabrisae/public_html/ruangprojek/sadiahcafe/mylife/public/index.php(601): Slim\App->run() #7 {main}
测试情况
测试路径:https://ruangprojek.com/sadiahcafe/mylife/public/allmenu
正常情况下API可访问时会返回参数缺失等其他错误,但当前始终返回404。已尝试修改链接、Base URL、.htaccess配置及数据库,均未解决问题;本地环境运行正常,文件名称也无大小写问题。
内容的提问来源于stack exchange,提问作者miss quratul aini
相关产品推荐
相关产品推荐

