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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.