using root-relative urls and problems with testing locally

佐手、 提交于 2019-12-12 05:39:57

问题


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.

Is there anyway around this problem?

MTIA.

(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.)


回答1:


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);

And then:

if(DEV_VERSION)
    define('URL', LOCAL_URL);
else
    define('URL', DISTANT_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.

And it's simple to add debug controls:

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



回答2:


The most handy solution would be to create a dedicated domain for the every test site.
I am sure XAMPP even have some tool for this task, making these few config file edits automated



来源:https://stackoverflow.com/questions/7975313/using-root-relative-urls-and-problems-with-testing-locally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!