我目前正在我的專案中制作路由器,但我意識到了一個問題。我正在使用 .htaccess 檔案將所有路由重定向到我的router.php檔案。這會產生一個問題,我的所有 css/images/js 檔案也都被重定向到我的 .htaccess 檔案。這是我的 .htacess 檔案的代碼:
RewriteEngine on
Options Multiviews
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(. )/?$ $1.php
RewriteRule ^(.*)$ router.php [NC,L,QSA]
我曾嘗試通過手動檢測它是否是可識別的靜態檔案型別之一并手動回傳內容來解決此問題。在我的路由器類中,我檢查了給定檔案型別是否是可識別的靜態檔案型別。如果它是靜態檔案型別,我將呈現它,否則我會將它交給句柄函式,該函式基本上檢查路由是否被識別并回傳對函式的呼叫。
if ($isStatic["check"]) {
$this->render->returnStaticFiles($data["uri"], $isStatic["type"], $isStatic["ext"] ?? "");
} else {
$this->handle($data["uri"]);
}
但是,這個問題是我需要手動將靜態檔案型別添加到陣列并使用函式來設定正確的標題,否則它只會被識別為text/html并且不適用于任何 css或影像。Css 不會被決議,影像只會以二進制文本的形式回傳,就像亂碼一樣。這就是我目前實作渲染類的方式:
<?php
namespace app\Router;
use JetBrains\PhpStorm\ArrayShape;
class Render
{
public array $static_file_types = ["js", "css", "html", "json"];
public array $image_types = ["png", "jpg", "jpeg"];
public function __construct(
public string $root_dir = "",
public string $home = "http://localhost/"
){}
public function throwError(int $statusCode = 500) :string
{
$err_dir = $this->root_dir . "errors\\";
$file_contents = file_get_contents($err_dir . "$statusCode.php") ?? false;
if (!$file_contents) {
return str_replace(
["{code}", "{home}"],
[$statusCode, $this->home],
file_get_contents($err_dir . "err_template.php")
);
}
return str_replace("{home}", $this->home, $file_contents);
}
public function returnStaticFiles(string $uri, string $type, string $ext) : void
{
if ($type == "cnt") {
$this->setHeader($ext);
include_once $this->root_dir . $uri;
} elseif ($type == "img") {
$this->handleImage($this->root_dir . urldecode($uri), $ext);
} else {
echo "This file is not supported sorry";
exit();
}
}
private function handleImage(string $path_to_image, string $type) : void
{
if (file_exists($path_to_image)) {
$image = fopen($path_to_image, "r");
header("Content-Type: image/$type");
header("Content-Length: " . filesize($path_to_image));
fpassthru($image);
} else {
echo "This image does not exist sorry";
}
}
#[ArrayShape(["check" => "bool", "type" => "string", "ext" => "string"])]
public function checkIsStatic($uri) : array
{
$_uri = explode(".", $uri);
$ext = end($_uri);
if (in_array($ext, $this->static_file_types)) {
return ["check" => true, "type" => "cnt", "ext" => $ext];
} elseif (in_array($ext, $this->image_types)) {
return ["check" => true, "type" => "img", "ext" => $ext];
} else {
return ["check" => false, "type" => ""];
}
}
private function setHeader($ext): void
{
switch ($ext) {
case "css":
header("Content-Type: text/css"); break;
case "js":
header("Content-Type: text/javascript"); break;
case "json":
header("Content-Type: application/json"); break;
default:
header("Content-Type: text/html"); break;
}
}
}
這作業得很好,但有什么辦法解決這個問題嗎?我可能需要提供 PDF 內容或其他檔案型別,例如音頻或視頻,我認為這不是最佳解決方案。我嘗試搜索它,但我認為我找不到任何東西。
我感謝任何幫助。
uj5u.com熱心網友回復:
原來這是一個簡單的解決方案。我只是將重定向所有內容更改router.php為另一行RewriteRule !.(js|css|ico|gif|jpg|png)$ router.php [L]
這會檢查檔案是否不是 js 或 css 等,然后它會重定向到 router.php 檔案,否則它通常會提供該檔案。我認為我的問題不夠清楚,其他人無法理解,所以我希望這個答案可以解決問題。
對這個鏈接的大喊大叫,它使一切都正確:在.htaccess中重寫規則:它做了什么
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/480193.html
上一篇:特定的htacces重定向例外
