我已经下载了一个完整的网站,将为客户升级。
我注意到代码无法找到文件(css、js、图像等)。
路径的编码如下 - 在 /
开头有一个正斜杠。
从每个 href、src 或其他内容的开头删除这些 /
解决了本地计算机上的问题,但当然在网上搞砸了。
这在本地不起作用:
href="/design/layout.css"
这将(删除第一个斜杠)
href="design/layout.css"
造成这种差异的原因是什么?
是否需要配置某种系统变量才能使两台服务器以相同的方式处理路径?
I have downloaded a complete website that will be upgrading for a client.
I have noticed the code is unable to locate files (css,js,images etc.).
The paths are coded like this — with a forward slash in the beginning /
.
Removing those /
from the beginning of each href, src or whatever
solves the issue on the local machine but of course messes things up online.
this will not work locally:
href="/design/layout.css"
this will (first slash removed)
href="design/layout.css"
What is the reason for this difference?
is there a system variable of some sort the a need to configure in order to get both servers to treat paths the same way?
发布评论
评论(1)
如果相对 URL 开头有
/
,则它将解析为域 Web 根目录的绝对路径。绝对路径示例(以 / 开头):
位于domain.com/test/other/path/test.html 上的文件中
href="/design/layout.css"
-> 搜索domain.com/design/layout.css
浏览器在domain/test.html文件中
href="/design/layout.css"
->浏览器搜索domain.com/design/layout.css
结论绝对路径
浏览器两次在同一位置进行搜索。
相对路径示例(开头不带 /):
在domain.com/test/other/path/test.html 上的文件中
href="design/layout.css"
-> 搜索domain.com/test/other/path/design/layout.css
浏览器在domain/test.html文件中
href="design/layout.css"
->浏览器搜索domain.com/design/layout.css
结论相对路径
浏览器在不同位置进行搜索。
解决方案
您可以添加 .conf 文件中的 ="nofollow">Apache Alias ,这会强制服务器在请求第一个路径时输出第二个路径的内容。在您的情况下,Apache 从新路径而不是 /design/ 输出 CSS 文件。
Apache 别名示例
If there is a
/
at the beginning of an relative URL then it is resolved as absolute path to the web root of a domain.Example absolute path (with / at the beginning):
in a file on domain.com/test/other/path/test.html
href="/design/layout.css"
-> browser searches atdomain.com/design/layout.css
in a file on domain/test.html
href="/design/layout.css"
-> browser searches atdomain.com/design/layout.css
Conclusion absolute paths
Browser searches at the same place both times.
Example relative path (without / at the beginning):
in a file on domain.com/test/other/path/test.html
href="design/layout.css"
-> browser searches atdomain.com/test/other/path/design/layout.css
in a file on domain/test.html
href="design/layout.css"
-> browser searches atdomain.com/design/layout.css
Conclusion relative path
Browser searches at different places.
Solution
You can add a Apache Alias in your .htaccess or .conf file, this forces the server to output the content of the second path when requesting the the first path. In your case, Apache outputs the CSS files from the new path instead of /design/.
Example of Apache Alias