using root-relative urls and problems with testing

2019-08-25 02:28发布

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

2条回答
ら.Afraid
2楼-- · 2019-08-25 03:12

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

查看更多
唯我独甜
3楼-- · 2019-08-25 03:24

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);
查看更多
登录 后发表回答