Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php htaccess尾部斜杠会打断css和js文件_Php_.htaccess_Relative Path - Fatal编程技术网

Php htaccess尾部斜杠会打断css和js文件

Php htaccess尾部斜杠会打断css和js文件,php,.htaccess,relative-path,Php,.htaccess,Relative Path,好吧,虽然这类htaccess问题被问了很多,但我找不到一个(部分)适合我的案例的解决方案 所以我有这个系统,可以放在任何系统中,比如localhost/system或localhost/folder/here 无论如何,系统使用index.php和.htaccess文件,像localhost/system/param1/param2/param3这样的url应该只引用index.php文件,但是,当调用时,放在localhost/system/css/中的样式表应该仍然可用 Htaccess:

好吧,虽然这类htaccess问题被问了很多,但我找不到一个(部分)适合我的案例的解决方案

所以我有这个系统,可以放在任何系统中,比如localhost/system或localhost/folder/here

无论如何,系统使用index.php和.htaccess文件,像localhost/system/param1/param2/param3这样的url应该只引用index.php文件,但是,当调用时,放在localhost/system/css/中的样式表应该仍然可用

Htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php [L,QSA]
index.php:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>MyPage</title>
    <link rel='stylesheet' type='text/css' href='css/style.css' />
    <script src='js/index.js'></script>
</head>

<body>
content...
</body>
</html>

我的网页
内容。。。
正如你所看到的,我喜欢保持路径的相对性。 当url被使用时,它会工作。。。但是,当url(如或。。它不再工作,因为链接将不引用正确的路径

我如何解决这个问题? 正如我所说,stack有多个主题,但我找不到一个解决相对路径的主题

答案部分由答案提供:

<?php
$basePath = str_replace('index.php', '', $_SERVER['PHP_SELF']);
echo "<base href='{$basePath}' />";
?>

根据您构建站点的方式,在这些情况下,设置基本URL通常是最简单的。将其作为
标记中的第二个元素,放在字符集声明之后:

<base href="https://example.com/" />

现在,页面上的所有内容都与
https://example.com/


(当然,可以替换任何你想要的基。如果你真的只是在任何地方使用站点的根作为基,你可以简单地删除它,并在引用CSS文件时使用完整路径,从
/
开始)

在它们前面加一个斜杠
/CSS/style.CSS
。然后,它应该使用根路径作为htaccess中的localhost/或localhost/system
RewriteBase/
/system/css/style.css
中的根路径作为
css
文件链接。啊,谢谢,在我的编辑中,我根据您的提示发布了解决方案。谢谢