如何在localhost和Web服务器上实现绝对URL? [英] How to implement absolute URLs on localhost and web server?

查看:100
本文介绍了如何在localhost和Web服务器上实现绝对URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用具有绝对网址路径的相对网址的以下链接做法:

I've typically used the following linking practice, of relative URLs with an absolute URL path:

<a href="/relative/path/to/document.html">

但我会实施绝对网址

<a href="http://example.com/relative/path/to/document.html">

我不需要更改它们(在HTML文档中自动查找和替换)。

It's not a problem for me to change them (automated find & replace in HTML documents).

但是最佳做法以确保它在我的localhost上都能 (支持PHP) ),以及在网络上?为什么?

But what is the best practice to make sure that it will work on both my localhost (which supports PHP), as well as on the web? And why?


例如,这里我是如何做的PHP包括:

For example, here's how I do PHP includes:

<?php include($_SERVER['DOCUMENT_ROOT']."/relative/path/to/document.html"); ?>

href 网址采用相同的方法?不同的PHP技术更好吗?很想知道原因。

Adopt the same approach for href URLs? Is a different PHP technique better? Curious to hear why.

推荐答案

在本地和Web主机上保持相同的目录结构,并尽可能使用相对URI和路径。
如果必须使用绝对URI,请在某处定义它,并在需要输出URI时使用常量,以避免使用查找和替换进行更改。

Keep the same directory structure locally and on the web host, and use relative URIs and paths whenever possible. If you must use an absolute URI, define it once somewhere and use the constant whenever you need to output a URI to avoid having to make the changes using find and replace.

<?php
    define('ROOT_URI', 'http://example.com/subdirectory');
?>

<a href='<?php echo ROOT_URI; ?>/relative/path/to/document.html'> document </a>

如果你有一个配置文件或其他一些你只定义一次的文件,那就更容易了。

It's easier if you have a configuration file or some other included file where you only define it once.

这篇关于如何在localhost和Web服务器上实现绝对URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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