當請求 url 中有一個點時,我收到一個 404 錯誤 Not found 頁面,上面寫著“在此服務器上找不到請求的資源 /error.example。” 是什么導致了這個錯誤,有沒有辦法修復它?我正在使用 Slim 框架并使用 php 內置服務器運行我的代碼。我將在下面放一個重新創建上述錯誤的示例代碼。
索引.php:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$app->get('/{name}/oth', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write($name);
return $response;
});
$app->run();
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
index.php 和 .htaccess 都位于公共檔案夾內,我運行我的代碼php -S localhost:8080 -t public/
當我轉到我的瀏覽器并輸入http://localhost:8080/name/oth它正確顯示名稱的地址但是當我輸入http://localhost:8080/name.ex/oth它時顯示我提到的錯誤
Not Found
The requested resource在此服務器上未找到 /name.ex/oth。
Edit:
Adding the requested information:
-Log for php -S localhost:8080 -t public/:
PHP 8.1.2 Development Server (http://localhost:8080) started
-Log for the request url http://localhost:8080/name/oth:
[::1]:63906 [200]: GET /name/oth
-Log for the request url http://localhost:8080/name.ex/oth:
[::1]:63917 [404]: GET /name.ex/oth - No such file or directory
If i replace index.php with echo 'here'; die; it displays "here" correctly in the browser.
uj5u.com熱心網友回復:
PHP在那里負責,而不是苗條
[::1]:63917 [404]: GET /name.ex/oth - No such file or directory
此日志訊息源自 php 內置 Web 服務器,而不是苗條 - 它隱含在“沒有此類檔案或目錄”中,該請求已被視為檔案路徑。

如果回應看起來像這樣(php Purple :)),即與您自己的腳本可能生成的任何內容不同,這也是它來自內置 Web 服務器的有力證據。
這很可能與 php 開發服務器的已知行為有關,將任何帶有 a.的 url 視為檔案路徑,并且不將請求轉發到任何 php 腳本 - 例如,請參閱這個舊問題。
指定路由器腳本
這里的修復很簡單,在啟動php 開發服務器時指定一個“路由器腳本” :
如果在啟動 Web 服務器時在命令列上給出了 PHP 檔案,則將其視為“路由器”腳本。
這也是 slim自己的檔案如何使用網路服務器,適應問題使用:
$ php -S localhost:8080 -t public/ public/index.php
隨著服務器使用顯式路由器腳本運行,這應該可以解決請求未到達 index.php 的直接問題:
$ curl -i http://localhost:8080/blah.ex/blah
HTTP/1.1 200 OK
Host: localhost:8080
Date: Sun, 20 Mar 2022 20:33:58 GMT
Connection: close
X-Powered-By: PHP/8.0.11
Content-type: text/html; charset=UTF-8
...whatever public/index.php returns...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/449234.html
標籤:php .htaccess slim php-builtin-server
上一篇:我無法更改我的流媒體網站主頁
