我想知道是否可以通过 .htaccess 或任何其他方式来设置基本 URL。
例子
我在 index.php 中的 CSS 是这样设置的
<link rel="stylesheet" href="css/style.css"/>
当我在第一个目录加载我的文件和页面时,一切正常。但是当我在/page/index.html 之类的文件夹中设置页面时。未加载 css,因为该文件夹不在 page/中,而是在根目录中。
因此是否可以告诉它总是从根目录查找以加载 CSS、图像、javascript 等,或者我必须为我要加载的每个元素设置完整的 url 路径(CSS、图像、javascript、等等)?
谢谢
最佳答案
使用基本 URL,例如:-
<html>
<head>
<base href="http://www.w3fools.com/" target="_blank" />
<!-- only one base element is allowed -->
<link rel="stylesheet" type="text/css" href="/stdtheme.css" />
</head>
<body>
<img src="stickman.gif" width="24" height="39" /> - Notice that we have only specified a relative address for the image. Since we have specified a base URL in the head section, the browser will look for the image at "http://www.w3schools.com/images/stickman.gif"
<br /><br />
<a href="http://www.w3fools.com">Don't use W3Schools</a> - Notice that the link opens in a new window, even if it has no target="_blank" attribute. This is because the target attribute of the base element is set to "_blank".
</body>
</html>
关于php - .htaccess 中的基本 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6848428/