【发布时间】:2019-01-25 10:17:52
【问题描述】:
好的,虽然这类 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 http://localhost/system/param1 时,它可以工作......但是当使用诸如 http://localhost/system/param1/ 或 http://localhost/system/param1/param2/etc.. 之类的 url 时,它不再工作,因为链接不会引用正确的路径
我该如何解决这个问题? 正如我所说,堆栈有多个关于此的主题,但我找不到一个解决相对路径的问题。
部分由答案提供的答案:
<?php
$basePath = str_replace('index.php', '', $_SERVER['PHP_SELF']);
echo "<base href='{$basePath}' />";
?>
通过将它放在我的 html 标头的顶部,它可以工作,它允许我在每次这样做时只需移动系统而无需编辑代码。
【问题讨论】:
-
在他们前面加上一个斜线
/css/style.css。然后它应该使用根路径。 -
@Sarcoma root 在 localhost/ 或 localhost/system 中
-
RewriteBase /在 htaccess 中,/system/css/style.css用于您的CSS文件链接。
标签: php .htaccess relative-path