将根相对链接与子域一起使用 [英] Using Root-Relative Links With Subdomain

查看:60
本文介绍了将根相对链接与子域一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个网站,在该网站上,前任开发人员已对与站点根目录相关的所有链接进行编码,每个链接中都有一个前导反斜杠:

I have inherited a website where the previous developer has coded all links relative to the site root, with a leading backslash in each link:

<link href="/css/file.css" />
<script src="/js/file.js"></script>

当网站托管在服务器上时,这非常有用,因为链接等同于:

This works great for when the site is hosted on a server, as the links will equate to:

http://www.example.com/css/file.css
http://www.example.com/js/file.js

但是,当从子文件夹中调用以进行本地测试时,我正在尝试使这些链接正常工作.具体来说,我正在使用 WAMP,并将整个代码移动到一个名为 site 的本地文件夹,位于 http://localhost:8080/site/.

However, I'm trying to get these links to work correctly when called from within a subfolder for local testing. Specifically, I'm using WAMP, and have moved the entire code to a local folder called site at http://localhost:8080/site/.

我无法使用 localhost 的根目录,因为 WAMP 在那里存储了各种文件(包括会被覆盖的索引).

I can't use the root of localhost, as WAMP stores various files there (including an index that would get overwritten).

正如 StackOverflow 上的许多帖子所建议的那样,显而易见的解决方案是简单地使用文件夹相关链接,例如:

The obvious solution, as many posts here on StackOverflow suggest, is to simply use folder-relative links, such as:

<link href="css/file.css" />
<script src="js/file.js"></script>

然而,在各种不同的文件中确实有数百个硬编码的根相对链接,因此如果可能的话,最好避免更改其中的每一个.

However, there are literally hundreds of hard-coded root-relative links in various different files, so it would be great having to avoid altering every single one of them if possible.

为了避免编辑每个链接,我尝试设置一个 HTML <base> 标签并直接指定文件夹:

To avoid having to edit every link, I've tried setting an HTML <base> tag and specifying the folder directly:

<base href="http://localhost:8080/site/">

然而,这似乎不起作用.

However, this doesn't seem to work.

是否与根相关链接不兼容?

Is <base> incompatible with root-relative links?

有什么方法可以让我轻松地让所有文件都引用 http://localhost:8080/site/ 而不必手动编辑它们预先存在的根目录相关链接?还是我必须手动将每个更新为文件夹相关的?

Is there any way I can easily have all files reference http://localhost:8080/site/ without having to manually edit each one of their preexisting root-relative links? Or will I have to manually update each one to be folder-relative?

推荐答案

是否与根相关链接不兼容?

Is <base> incompatible with root-relative links?

不,但绝对路径仍然是绝对路径.它将通过删除 /site/ 相对于 http://localhost:8080/site/ 进行解析.

No, but an absolute path is still an absolute path. It will resolve relative to http://localhost:8080/site/ by dropping /site/.

如果你想使用绝对路径而不是将你的开发站点放在子目录中,那么 配置您的 HTTP 服务器以使用虚拟名称托管.

If you want to use absolute paths and not keep your development sites in subdirectories, then configure your HTTP server to use Virtual Name Hosting.

添加自定义主机名(在 LAN 的 DNS 服务器中或在开发系统的 hosts 文件中),例如 site.localhost,并设置 DocumentRoot 在虚拟主机中.

Add custom hostnames (either in the DNS server for your LAN or in the hosts file on your development system), such as site.localhost, and set the DocumentRoot in a virtual host.

这篇关于将根相对链接与子域一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆