各个域上的 HTML 相关链接
我有一个快速说明:
当您编码/开发主题时,如何链接到 html/css 代码中的各种文件?
例子: 我们公司主要在主模板中使用
,然后只使用
但是,当在不同的机器上进行测试时,我们在编码器的主开发站上使用 IP 地址而不是 localhost,因此所有基本链接都不起作用(因为 localhost 会转到我们网络中的查看机器,而不是编码器的机器)。
更新页面时也会发生同样的事情 - 在开发服务器上,我们必须编辑基本目标,因此浏览网站不会将我们带到实时网站 - 这部分实际上相当简单 PHP (if ... echo else echo some else
),但它仍然没有解决更多编码测试问题。
所以,我的问题是,你如何解决它?如何使用相对链接,它基本上不关心页面所在的域,也不关心 url 重写? (因为 ../images/
与 /
不同,与 /something/somethingElse/page
不同)?
I have quickie:
When you code/develop themes, how do you link to various files in your html/css code?
Example:
We at our firm use mostly <base target="http://whatever">
in our main template and then just <img src="./images/file.png">
in our html, "/category/page"
as links and something alike in our css.
However, when testing on different machines, we use ip address rather than localhost on main dev station of coder, so all base links don't work (because localhost goes to viewing machine, not coder's, in our network).
Same thing happens when updating pages - on dev server, we have to edit base target, so browsing site won't take us to live site - this part is actually rather simple PHP (if ... echo else echo something else
), but it still not solve problem of more coding-testing problems.
So, my question is, how do YOU solve it? How do you use relative links, which basically don't care for what domain is the page on and don't care for url rewrite? (because ../images/
is different for /
and different for /something/somethingElse/page
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我很熟悉你上面描述的问题。当您有多个环境(开发、测试、生产等)时,这是一个特殊的问题......我已经尝试了上面每个人建议的大部分内容,但发现它们仅部分有效。为了最终解决这个问题,我所做的就是创建一个自定义控件,我称之为“AnchorDomain”。该控件仅允许您将“/category/page”或“/images/file.png”等路径放入其 VirtulPath 属性中,它将根据您运行的环境动态为您构建正确的 url你的项目。这样,对于我的项目中的所有问题 url,我只需使用此控件,并且能够独立于环境运行我的代码,并且我的所有 url 都可以使用正确的名称进行解析。例如,这里是 aspx 页面中的语法示例。
所以这种情况下无论你在什么环境下这个链接总会显示正确的地址。
乍一看,这样做似乎有点矫枉过正,但这个控件在我遇到的所有情况下都解决了这个问题。
请参阅下面的代码:
享受吧!
I am familar with the issue you describe above. This is a particular issue when you have several environments, Development, Test, Production, etc... I have tried most of what everyone has suggested above, but have found them to be only partially effective. What I did to finally solve this issue was to create a custom control that I call an "AnchorDomain". This control simply allows you to put in a path such as "/category/page" or "/images/file.png" into its VirtulPath propery and it will dynamically build the correct url for you based on the environment in which you are running your project. This way for all the problem url in my project I simply use this control and am able to run my code independent of the environment and all of my url resolve with the correct name. So for instance here is an example of the syntax in a aspx page.
So in this case no matter what environment you are in this link will always show the correct address.
At first it might seem like overkill to do it this way, but this control solves this issue in all cases i have run across.
See code below:
Enjoy!
我现在在几个项目中采用的方法是创建一个 rootPath 变量,其中包含适量的
../
以到达根目录。像这样的东西......这对我们来说非常适合,因为我们的 htaccess 文件的设置方式。不过,不需要太多的摆弄就可以让它与标准页面一起工作。
The way I've done it for a few projects now is to create a rootPath variable that contains the right amount of
../
's to get to the root. Something like this....This works perfectly for us because of the way our htaccess file is set up. It wouldn't take much fiddling to get it to work with standard pages though.
就我个人而言,我总是使用绝对路径(即
/path/to/file.css
)并完全避开base
。您是否考虑过编辑
hosts
文件以将 IP 地址映射到本地主机名?Personally I always use absolute paths (i.e.
/path/to/file.css
) and steer clear ofbase
altogether.Have you considered editing the
hosts
file to map IP addresses to host names locally?就我个人而言,我不喜欢使用基地,因为它会导致您遇到的那种问题。绝对路径总是最好的,因为它们是明确的。如果您想说切换主题或 javascript 库版本或您的应用程序可以动态生成的链接以反映这一点,例如:
Personally I dislike the use of base as it leads to the sort of issues you are experiencing. Absolute paths are always best as they are unambiguous. If you want to say switch themes or javascript library versions or whatever your links could be dynamically genererated by your application to reflect this, e.g: