如何将相对路径指向Apache的目录更改?

发布于 2025-02-12 18:05:41 字数 413 浏览 0 评论 0原文

我有一个网站在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性不性感 2025-02-19 18:05:41
 /images/example.png
 

这是一个 root-relative ,而不是相对 url路径。

这是在尝试从http://example.com/images/example.png加载图像,但我实际上希望它从http://example.com提供服务。 /~user1/images/example.png

这是客户端/浏览器URL分辨率问题。服务器上没有“开关”可以应用于不更改实际URL的情况下更改此行为。

这是使用 apache per-user per-use per-use web目录

您的选项是:

  1. 请勿使用Apache Per-user Web目录。 IE。从URL中删除/〜&lt; user1&gt;/

或者,

  1. 使用相对 url paths与您的资产使用。例如。 图像/示例.png(无斜线前缀)。因此,浏览器正确解析为/~~user1/images/example.png而不是(至少从文档root访问时)。但是,如果您在不同的路径深度或使用页面等中使用常见模板等的页面上的网址,这会产生其他问题。
/images/example.png

This is a root-relative, not a relative URL-path.

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.

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:

  1. Don't use Apache per-user web directories. ie. Remove /~<user1>/ from the URL.

Or,

  1. Use relative URL-paths to your assets. eg. 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文