Jump to content

Set a Base Path for each site in localhost


Omzy

Recommended Posts

I've got a localhost Apache server on my Windows machine

 

I want to be able to create multiple sites under http://localhost, so for example:

 

http://localhost/site1

http://localhost/site2

http://localhost/site3

 

The problem with this is that relative URLs don't always work, for example if I have a have a common header file (header.php) which contains a reference to a stylesheet (<link rel="stylesheet" type="text/css" href="styles.css" />) and I include() header.php on all pages, it won't work for pages that are in a subfolder of that site, for example: http://localhost/site1/subfolder1/index.php

 

index.php includes header.php but the stylesheet reference points to the current folder, however styles.css is in the "root" of that site. If I change "styles.css" to "/styles.css" it looks in the root of "localhost" rather than the root of the site.

 

So is there any way to specify in Apache/PHP what the base path of the site should be? I've tried "RewriteBase" in .htaccess but that doesn't quite work..

You will have to setup a "config" to say for each site1 etc.

 

Then before including header.php include that config.php which contains constants or variables, such as "SITE"

 

IE:

http://localhost/site1/config.php

define("SITE", "site1");

 

http://localhost/site1/index.php

<?php
require('config.php');
require('../header.php');
?>

 

http://localhost/header.php

<base href="http://localhost/<?php echo SITE; ?>/" />
<link href="style.css" type="text/css" />

 

Catch the drift? You will have to define it somehow, or else the script is just like anything else, it does not know how to decide what "you" want.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.