我通常使用以下链接实践,属于 relative URLs with an absolute URL path :
<a href="/relative/path/to/document.html">
但我会实现绝对 URL:
<a href="http://example.com/relative/path/to/document.html">
更改它们对我来说不是问题(在 HTML 文档中自动查找和替换)。
但是什么是最佳实践以确保它能够在我的本地主机(支持 PHP)以及在网络上强>?为什么?
例如,下面是我如何使用 PHP 包括:
<?php include($_SERVER['DOCUMENT_ROOT']."/relative/path/to/document.html"); ?>
对 href
URL 采用相同的方法?不同的 PHP 技术更好吗?很想知道为什么。
最佳答案
您可以只在 HTML href 中包含一个预定义的常量或变量。
例如
<?php
define("LOCAL", "http://localhost");
define("WEB", "http://foo.bar");
$environment = LOCAL; //change to WEB if you're live
?>
<a href="<?php echo $environment; ?>/relative/path/to/document.html">
关于php - 如何在本地主机和 Web 服务器上实现绝对 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14371599/