如何将相对路径指向Apache的目录更改?
我有一个网站在Apache2服务器上运行,并在用户目录中提供服务。 URL看起来像这样:http://example.com/~user1
这将在我的服务器上提供/home/home/user1
的服务器上的文件。我面临的问题是,整个网站中的许多资产URL都以这样的相对路径的形式放入HTML:< img src =“/images/images/example.png” alt =“”/&gt ;
这是在尝试从http://example.com/images/example.png
加载图像,但我实际上希望它从http://example.com/~user1/images/example.png
。
我该怎么做?
I have a website running on an apache2 server and serving out of a user directory. The URL looks like this: http://example.com/~user1
This serves up files on my server located at /home/user1
. The problem that I am facing is that a lot of the asset URLs throughout the site are put into the HTML as relative paths like this: <img src="/images/example.png" alt="" />
This is attempting to load the image from http://example.com/images/example.png
, but I actually want it to serve from http://example.com/~user1/images/example.png
.
How can I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个 root-relative ,而不是相对 url路径。
这是客户端/浏览器URL分辨率问题。服务器上没有“开关”可以应用于不更改实际URL的情况下更改此行为。
这是使用 apache per-user per-use per-use web目录。
您的选项是:
/〜&lt; user1&gt;/
。或者,
图像/示例.png
(无斜线前缀)。因此,浏览器正确解析为/~~user1/images/example.png
而不是(至少从文档root访问时)。但是,如果您在不同的路径深度或使用页面等中使用常见模板等的页面上的网址,这会产生其他问题。This is a root-relative, not a relative URL-path.
This is a client-side/browser URL resolution issue. There are no "switches" on the server that can be applied to change this behaviour, without changing the actual URLs.
This is an inherent problem with using Apache per-user web directories.
Your options are:
/~<user1>/
from the URL.Or,
images/example.png
(no slash prefix). So the browser correctly resolves this as/~user1/images/example.png
instead (when accessed from the document root at least). But this creates additional problems if you are rewriting the URL of your pages at different path depths or using common templates in your pages etc.