gpt4 book ai didi

php - 如何使用 PHP 路由和 .htaccess 包含 CSS/JS 和其他文件

转载 作者:行者123 更新时间:2023-11-28 02:45:37 27 4
gpt4 key购买 nike

我正在尝试使用 PHP 路由类在 HTML 上应用 CSS,但它不起作用...

我想要包含H1 红色样式,这是代码...

.htaccess

RewriteEngine On
RewriteRule ^(.*)$ index.php?p=/$1 [QSA,L]

index.php

<?php
class R
{
private $r = [];
function a($r, callable $c)
{
$this->r[$r] = $c;
}

function e() {
$p = $_GET['p'];
$k = isset($this->r[$p]) ? $this->r[$p] : $this->r[''];
$k();
}
}

$router = new R;

$router->a('/', function(){
include "page/home.php";
});

$router->e();
?>

home.php

<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Colorized.</h1>
</body>
</html>

style.css

h1 {
color: red;
}

如何使用添加到 HTML 中的任何 CSS/JS 文件使其工作?

最佳答案

看起来您的规则是将 css 和 js 文件重写回 index.php,这就是它们未加载的原因。您需要从规则中排除这些文件。

你可以使用这个:

RewriteEngine on
RewriteRule ^((?!\.js|\.css).*)$ /index.php?p=/$1 [L]

要修复相对 uris,请在网页的 head 部分添加以下内容

<base href="/">

关于php - 如何使用 PHP 路由和 .htaccess 包含 CSS/JS 和其他文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41693757/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com