简体   繁体   English

htaccess的尾部斜杠会破坏CSS和JS文件

[英]htaccess trailing slashes break css and js files

Ok so although this sort of htaccess questions are asked a lot, i could not find a (partial) solution that worked for my case. 好的,尽管提出了很多此类htaccess问题,但我找不到适合我的情况的(部分)解决方案。

So I have this system that can be placed in any system such as localhost/system or localhost/folder/here 所以我有这个系统,可以放在任何系统中,例如localhost / system或localhost / folder / here

anyhow, the system uses a file index.php and .htaccess file and an url like localhost/system/param1/param2/param3 should just refer to the index.php file however, the stylesheet placed in localhost/system/css/ should still be available when called upon. 无论如何,系统使用文件index.php和.htaccess文件,并且像localhost / system / param1 / param2 / param3之类的url应该仅引用index.php文件,但是,放置在localhost / system / css /中的样式表仍应在需要时可用。

Htaccess: .htaccess中:

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

index.php: 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>

As you can see, I like to keep the paths relative. 如您所见,我喜欢保持路径相对。 When the url http://localhost/system/param1 is being used it works... however when an url such as http://localhost/system/param1/ or http://localhost/system/param1/param2/etc .. is used it no longer works because the links will not refer to the correct path 当使用URL http:// localhost / system / param1时 ,它可以工作...但是,当使用诸如http:// localhost / system / param1 /http:// localhost / system / param1 / param2 / etc之类的URL时使用..后将不再起作用,因为链接不会指向正确的路径

How can i solve this issue? 我该如何解决这个问题? As I said, stack has multiple topics about this but i couldn't find one solving relative paths. 就像我说的那样,堆栈对此有多个主题,但是我找不到解决相对路径的方法。

The answer partly provided by the answers: 答案部分由答案提供:

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

By placing this on the top of my html header it worked and it allowed me to simply move the system without editing code each time I do so. 通过将其放在我的html标头的顶部,它可以工作,并且每次我这样做时,我都可以简单地移动系统而无需编辑代码。

Depending on how you've built your site, it's often easiest in these cases to just set a base URL. 根据您构建网站的方式,在这种情况下,通常最简单的方法就是设置基本URL。 Put this as the second element in your <head> tag, right after your character set declaration: 在字符集声明之后,将其作为<head>标记中的第二个元素:

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

Now, everything on the page is relative to https://example.com/ . 现在,页面上的所有内容都是相对于https://example.com/

(Of course, substitute in whatever base you want. If you really are just using the root of the site as the base everywhere, you can simply drop this and use full path when referencing your CSS files, starting with / .) (当然,可以替换为您想要的任何基础。如果您确实只是将站点的根目录用作各处的基础,则可以简单地将其删除,并在引用CSS文件时使用完整路径,以/开头。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM