Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Root path in XAMPP

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?

like image 637
JohnnyFaldo Avatar asked Dec 11 '13 11:12

JohnnyFaldo


People also ask

Where is the root directory in XAMPP?

The document root (web root) directory is located at /opt/lampp/htdocs/ . All files placed in this directory will be processed by the web server. To host other files on your system with XAMPP, you can configure an alias with Apache. Edit Apache's /opt/lampp/etc/httpd.

What is path in XAMPP?

First, locate the executable PHP file called php.exe . I have installed XAMPP, which is installed on the location C:\xampp\ and the location of my php.exe file is in C:\xampp\php\php.exe.

What is the default Dir directory of XAMPP?

In the xampp PHP server, files are served by default from c:/xampp/htdocs directory.


2 Answers

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.

like image 107
Chris Avatar answered Oct 14 '22 08:10

Chris


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';
like image 29
Philipp Avatar answered Oct 14 '22 09:10

Philipp