Aptana Studio 3 预览绝对路径问题
我的项目有这样的结构:
Root Directory
|-css folder
|-style.css
|
|-it folder
|-index.html
如果我尝试包含以下 css 文件:
<link href="/css/style.css" rel="stylesheet" type="text/css"/>
来自 index.html、aptana 预览以及内部服务器找不到 style.css。 这是为什么呢? 在我的远程服务器中,它工作得很好,我不想使用相对路径。
I have this structure for my project:
Root Directory
|-css folder
|-style.css
|
|-it folder
|-index.html
If I try to include css file with:
<link href="/css/style.css" rel="stylesheet" type="text/css"/>
from index.html, aptana preview and also internal server can not find style.css.
Why is this?
In my remote server it works perfectly and I do not want to use a relative path.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就“为什么”而言,您遇到的问题与开发服务器与生产服务器的设置方式有关。
假设采用标准设置,您的生产服务器将收到对域(即 http://mysite.com)的请求,该域是,由于缺乏更好的词,映射到服务器上的文件夹(即,对 http://mysite.com 将映射到您的文件夹 /var/www/mysite 服务器)。
因此,当您使用 /css/style.css 链接到样式表时,您的(生产)服务器会立即转到 /var/www/mysite 文件夹并开始查找 css 文件夹、文件等。正如您所指出的,这没有问题。
然而,您的开发计算机在本地提供页面,并且具有不同的目录结构用于映射到文件和文件夹。
当我在 Aptana 项目中打开 HTML 页面并点击预览按钮时,Studio 会加载 http:// /127.0.0.1:8020/mysite/public/404.html(注意IP和端口之后的第一个文件夹是mysite)。要加载绝对路径的 CSS 文件,本地服务器实际上正在寻找 http://127.0.0.1: 8020/css/styles.css 但它需要到达 http://127.0.0.1:8020/mysite/css/styles.css。
链接(/css/styles.css)中的第一个“/”告诉服务器转到服务器的根目录并从该点开始查找文件夹和文件...但是在该目录中没有 css 文件夹本地服务器的根目录。它位于 /mysite/css/styles.css 中,这就是为什么 fskreuz 建议使用相对路径并使用“../css/styles.css”。
就我个人而言,我更喜欢绝对链接(但这只是个人喜好,并不以任何方式挑战或评论 fskreuz 的回应)。然而,我的本地开发设置有利于使用它们,因为我为我工作的网站设置了虚拟主机。我使用 Apache 为每个项目设置了一个虚拟主机。有了这个,我可以在我的计算机上的任何浏览器中加载类似 http://dev.mysite.com 的内容并进行测试我的网站/应用程序以使其反映我的生产设置的方式。
In terms of the "why", the problem you are having is related to how your development server is setup versus your production server.
Assuming a standard setup, your production server will receive requests for a domain (i.e., http://mysite.com) that is, for lack of a better word, mapped to a folder on your server (i.e, a request to http://mysite.com will be mapped to a folder, /var/www/mysite, on your server).
So, when you link to a style sheet with /css/style.css, your (production) sever immediately goes to the /var/www/mysite folder and starts looking for the css folder, file and so on. No problems with that, as you point out.
Your development machine, however, is serving up pages locally and has a different directory structure for mapping to files and folders.
When I open an HTML page in my Aptana project and hit the preview button, Studio loads http://127.0.0.1:8020/mysite/public/404.html (note how the first folder after the IP and port is mysite). To load the absolutely pathed CSS file, the local server is actually looking for http://127.0.0.1:8020/css/styles.css but it needs to get to http://127.0.0.1:8020/mysite/css/styles.css.
The initial "/" in your link (/css/styles.css) tells the server to go to the root directory of the server and start looking for the folder and files from that point ... but there is no css folder in the local server's root directory. It lives in /mysite/css/styles.css and that's why fskreuz suggests relative paths and using "../css/styles.css" instead.
Personally, I prefer absolute links (but that's just a personal preference and not in any way a challenge to or comment upon fskreuz's response). However, my local development setup is conducive to using them because I setup virtual hosts for the sites I work on. Using Apache, I setup a virtual host for each of my projects. With this, I can load something like http://dev.mysite.com in any browser on my computer and test my site/app in a way that makes it mirror my production setup.