Root path in XAMPP

14,306

Solution 1

An alternate way to handle this is with the use of Virtual Hosts.

A virtual host acts like a second version of localhost that works specifically for a subfolder. For example, you could set up Apache so that when you visit http://example (no .com or anything), it shows you the content from http://localhost/example/. All CSS and JavaScript and links would act as if they were operating from the root folder of a website, since the leading example folder has been trimmed out of the URL.

I can't find a walkthrough that I used to use for XAMPP, but here a similar one that covers all of the main points. It was written for Windows, but I imagine that there are similar mechanisms that you can use for LAMP:

To summarize, here's what the article tells you to do:

  • Enable Virtual Hosts within Apache
  • Set it up so that when you visit example, you are sent to 127.0.0.1
  • Configure Apache so that when someone visits 127.0.0.1 (but the name of the website is example), then it shows content from the example folder.

This is how your production site (which is a single server with multiple websites) has a different "root" for each website.

Solution 2

Have you ever thought about the base-tag in the header of your html content? http://www.w3schools.com/tags/tag_base.asp

<head>
    ...
    <base href="<$path />">
    ...
</head>

get base path:

$path = $_SERVER['SERVER_NAME'] == 'production.host' ? '/' : 'projectName';

Solution 3

You can either try the solution provided by others in this thread (which are programming solutions), or as an alternative, I do something different (a setup solution).

For me, I like to create an independent environment, in which my projects and development files are separate from XAMPP as much as possible (I am using XAMPP, but the principle applies to other hosting environments). This allows me to easily install new updates for XAMPP whenever they become available without worrying about my projects, and also I like to have all my projects in one folder dedicated to development. This development folder will contain projects for web, mobile, and other environments.

The way I set it up, is I have a c:\dev\ folder, that will contain a list of my projects, each project is on its own. So, for example, c:\dev\project1\, c:\dev\project2\ and so on.

Now, after I create those folders for development, I make sure that the httpd-vhosts.conf file (located at c:\xampp\apache\conf\extra\) and the hosts file (located at c:\Windows\System32\drivers\etc\) have the correct references.

Lets assume that one of my projects is called project1. and it is typically located at c:\xampp\htdocs\project1 then I would normally access it via the browser as http://localhost/project1

However, in order to have an independent environment, and as explained earlier, I would create a development folder in the c: drive called c:\dev, then I would move project1 into it, and ended up with c:\dev\project1

Then, to access this project via typing project1.dev in the web browser, I appended the httpd-vhosts.conf file located at c:\xampp\apache\conf\extra\ as follows:

<VirtualHost project1.dev:80>
    ServerAdmin [email protected]
    DocumentRoot "C:/dev/project1"
    ServerName project1.dev
    ServerAlias www.project1.dev
    ErrorLog "logs/project1.dev.error.log"
    CustomLog "logs/project1.dev.access.log" combined

    <Directory "C:/dev/project1">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>

</VirtualHost>

Also, I have to update the hosts file (located at c:\Windows\System32\drivers\etc\, and add the following entry:

127.0.0.1    project1.dev
127.0.0.1    www.project1.dev

(where 127.0.0.1 is the same as your localhost. Also note that you have to add the second entry in the hosts file for the www alias).

This allows me to access my project as: http://project1.dev

Once this is set, I can now write my code consistently for both my development and production environments, and my references to the root will work just fine.

Also, with this setup, I do not care if I need to update my XAMPP or switch to LAMP or anything else, all I care about is making sure I take care of one file only, which is my setup file httpd-vhosts.conf. And as I mentioned, I always prefer having a separate folder for development, and I can have different types of projects in the development environemt, for example, mobile projects, web projects, ... etc.

Hope this helps.


Note about multisite (subdomain setup)

If you would like to setup a multisite (subdomain setup), then after you enable WordPress for multisite according to the WordPress instructions, you have to do the following:

Assuming you want the following structure for multisite:

project1.dev
www.project1.dev (this is an alias to project1.dev)
sub.project1.dev (this is another setup, subdomain)

Then you have to add the following entry to httpd-vhosts.conf

<VirtualHost sub.project1.dev:80>
    ServerAdmin [email protected]
    DocumentRoot "C:/dev/project1"
    ServerName sub.project1.dev
    ErrorLog "logs/sub.project1.dev.error.log"
    CustomLog "logs/sub.project1.dev.access.log" combined

    <Directory "C:/dev/project1">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>

And update the hosts file to include

127.0.0.1    www.project1.dev

Note that DocumentRoot points to the same directory for both the main site and the subdomain site.

Repeat the process for each new subdomain you add in your WordPress network.

Solution 4

I think the best way to do this is with a baseurl, set your baseurl in your config and use it for external files.

$config = new Config();
public $baseurl = "http://dev050.nl";

You can use it then as

$config->baseurl;

And maybe this is something interesting for you: http://twig.sensiolabs.org/

hope it helped.

Solution 5

I do a similar thing to switch databases between my dev server and the hosting server if it's any use to you. No reason why it shouldn't work for a base URL.

// db connect
if($_SERVER['SERVER_NAME'] != "dev.mydomain.org"){
    try{
        $pdo = new PDO('mysql:host=mysql.***.net;dbname=***;charset=utf8', '***', '***');
    }catch(PDOException $ex){
        header('Location: error_db.php');
    }
}else{
    try{
        $pdo = new PDO('mysql:host=localhost;dbname=***;charset=utf8', '***', '***');
    }catch(PDOException $ex){
        header('Location: error_db.php');
    }
}
Share:
14,306
JohnnyFaldo
Author by

JohnnyFaldo

Updated on June 20, 2022

Comments

  • JohnnyFaldo
    JohnnyFaldo almost 2 years

    I've had this problem for a while, and have unsuccessfully searched far and wide for an answer.

     <img src="/images/test.jpg" /> 
    

    Gets an image from (root path - in my case in production in LAMP)

     htdocs/images/test.jpg 
    

    Whether it's called from htdocs/index.php or htdocs/foo/bar/index.php

    I use XAMPP in development, and inside htdocs have project folders, so the method described above although will work when live requires me to alter it to:

    <img src="/projectName/images/test.jpg" /> 
    

    when working locally.

    To make this simpler i define a constant BASE, which in development I use:

    define('BASE','/projectname/) 
    

    And then when it's live I change to:

    define('BASE','/')
    
    <img src="<?php echo BASE;?>images/test.jpg" />
    

    This is obviously really annoying and ends up causing several issues. Please can someone shed some light on this situation, what I'm specifically looking to do is use root path in my image/script sources but for:

    <img src="/images/test.jpg" />
    

    when called from htdocs/projectName/foo/test.php

    to look for the image in:

    htdocs/projectName/images/
    

    Is this possible?

  • JohnnyFaldo
    JohnnyFaldo over 10 years
    Thanks, but I'm looking for a solution that won't need to edit to make live.
  • AeroX
    AeroX over 10 years
    @JohnnyFaldo This solution should work for you. All you need to do is create a config.php file with all your deployment specific variables in it and include() it in your PHP files.
  • JohnnyFaldo
    JohnnyFaldo over 10 years
    @AeroX That's what I currently do, I have config.php which amongst other things defines the base url and is included across the board. What I need is a way to (or an alternative to) xampp using the sub folder in htdocs where the project lives as the base root, rather than it's parent folder httpdocs
  • JohnnyFaldo
    JohnnyFaldo over 10 years
    yeah defining it there or how I currently do still results in needing to edit it's definition in between development and production, as it still changes
  • Philipp
    Philipp over 10 years
    To choose between production and developement mode, you can use $_SERVER['SERVER_NAME']
  • JohnnyFaldo
    JohnnyFaldo over 10 years
    Thank you this is exactly what I was looking for, will award bounty in 23 hours.
  • JohnnyFaldo
    JohnnyFaldo over 10 years
    just implemented this and it works perfectly, thanks again @Chris