0

I am sure that this is a repost but I cannot find a question the same as what I want to find out. Essentially, whenever I am working offline, all URIs that I use across the site refer to offline locations eg 127.0.0.1/home.html however, when I go to upload the site, these URIs need to be changed to their equivalents eg example.com/home.html and I either need to go through all of the pages and update these references or use some php to insert the correct address at every point where an address is used. At the minute I am using something like this:

Top of every page:

<?php $offline = false; ?>

Link:

<a href="<?php echo ($offline ? '127.0.0.1' : 'example.com'); ?>/home.html">Home</a>

But this seems like a poor way to achieve something which should be relatively simple. What is the standard way of keeping these references up to date. I considered using relative links everywhere but that proved to have problems (for example view includes don't work correctly) and I tried setting the base href to the homepage but that threw up other problems.

Any suggestions would be very much appreciated. Thanks.

4

4 回答 4

1
$server=$_SERVER['SERVER_ADDR'];
// Local
if(strstr($server,'127.0.0'))
{
    define('ROOT_PATH','http://127.0.0.1/');
}
// Server
else
{
    define('ROOT_PATH','http://www.yoursite.com/');
}

将此代码放在 php 文件中并包含在每个页面中。

接着:

<a href="<?php echo ROOT_PATH?>/home.html">Home</a>

在这种情况下,您也可以将文件放在文件夹中,例如:

define('ROOT_PATH','http://www.yoursite.com/mysite/');
于 2013-04-23T13:10:50.750 回答
0

我发现解决此问题的一种巧妙方法是修改您的 HOSTS 文件,使其将 example.com 指向 127.0.0.1,以便您可以在任何地方引用 example.com,但在您的开发完成之前被重定向到 127.0.0.1 .

于 2013-04-28T11:34:18.227 回答
0

正如 Waygood 所建议的,听起来您需要将路径从绝对路径更改为站点根相对路径或相对路径。示例:如果使用站点根目录,则 127.0.0.1/home.html 将变为 /home.html。或者,如果有必须包含绝对路径的原因,您可以在服务器上设置环境变量或指示环境类型的 PHP 常量,并根据此值切换链接。使用环境变量的额外好处是能够在您的开发和生产机器上保持完全相同的代码库,而不必求助于主机或 IP 检测。这是我使用的 zend 框架文档指南中的一个示例:

defined('APPLICATION_ENV') || define('APPLICATION_ENV',
    (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

来自: http: //framework.zend.com/manual/1.12/en/zend.application.quick-start.html

于 2013-04-23T13:07:20.080 回答
0

Another way you could do it is using relative paths.

For files in the same directory you'd use ./ and for files above that directory use ../ This works for anchors in HTML and requires and includes in PHP.

于 2013-04-23T13:44:08.920 回答