使用相对于根的URL和本地测试问题 [英] using root-relative urls and problems with testing locally

查看:69
本文介绍了使用相对于根的URL和本地测试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XAMPP在本地测试站点,但是在使用root相对URL时遇到问题,因为root是 http://localhost/而不是存储站点文件的 http://localhost/test-site/.显然,当我将站点上传到远程服务器时,一切正常,但是当甚至没有加载样式表时,这也会使本地测试变得烦人.

I'm using XAMPP to test sites locally but am having problems using root-relative urls as root is http://localhost/ rather than http://localhost/test-site/ where the site files are stored. Obviously, when I upload the site to the remote server all works fine, but it makes testing locally irritating when the stylesheet isn't even being loaded.

反正有这个问题吗?

MTIA.

(Mohamed,我不确定为什么您要编辑标签-我的帖子中没有提及php,所以我不知道为什么您将php添加为标签.此外,该帖子涉及html和localhost ,这就是为什么我使用"html"和"localhost"作为标记并恢复标记的原因.如果我错误地标记了帖子,我将感谢您解释为什么以及如何确保以后正确标记帖子的解释. )

(Mohamed, I'm not sure why you edited my tags - there was no mention of php in my post so I don't know why you added php as a tag. In addition, the post relates to html and localhost, which is why I used "html" and "localhost" as tags and am reinstating the tags. If I am incorrectly tagging posts, I'd appreciate an explanation why and how so I can ensure I correctly tag posts in the future. Thanks.)

推荐答案

您可以在设置文件或index.php中使用常量:

You can use constants in a settings file or in index.php:

define('LOCAL_URL', 'http://localhost/test-site/');
define('DISTANT_URL', 'http://domain.tld/');
define('DEV_VERSION', true);

然后:

if(DEV_VERSION)
    define('URL', LOCAL_URL);
else
    define('URL', DISTANT_URL);

因此,您可以简单地在代码中使用URL常量,例如:

So you can simply use the URL constant in your code, for instance:

<link rel="stylesheet" type="text/css" href="<?php echo URL; ?>style/site.css" />

优点是它可以在所有情况下正常工作.

The advantage is that it works in all cases.

添加调试控件很简单:

if(DEV_VERSION)
    error_reporting(E_ALL ^ E_DEPRECATED ^ E_USER_DEPRECATED);
else
    error_reporting(0);

这篇关于使用相对于根的URL和本地测试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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