Skip to content Skip to sidebar Skip to footer

Using Root-relative Urls And Problems With Testing Locally

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

Solution 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:

<linkrel="stylesheet"type="text/css"href="<?phpecho 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);

Solution 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

Post a Comment for "Using Root-relative Urls And Problems With Testing Locally"