Categories
apache php server-configuration xampp

Make XAMPP / Apache serve file outside of htdocs folder [closed]

328

Is it possible to configure xampp to serve up a file outside of the htdocs directory?

For instance, say I have a file located as follows:

C:\projects\transitCalculator\trunk\TransitCalculator.php

and my xampp files are normally served out from:

C:\xampp\htdocs\

(because that’s the default configuration) Is there some way to make Apache recognize and serve up my TransitCalculator.php file without moving it under htdocs? Preferably I’d like Apache to serve up/have access to the entire contents of the projects directory, and I don’t want to move the projects directory under htdocs.

edit: edited to add Apache to the question title to make Q/A more “searchable”

6

  • 1

    The tags are supposed to be searchable enough, I think.

    – icedwater

    Jun 11, 2014 at 12:24

  • 18

    Andrew Koper, I agree, but it is unsurprising with the level of heavy-handed unfriendliness endemic throughout the stack-exchange ecosphere at this point. ¯_(ツ)_/¯

    – cmcculloh

    Nov 12, 2014 at 20:37

  • Maby off topic, but just want to tip: Some times an option is to instead move directories into your “htdocs” folder eg. if you would like to be able to open files in Google Drive in localhost 😉 (stackoverflow.com/questions/41751330/…)

    – Gordova

    Jan 19, 2017 at 22:26

  • On ubuntu a sym link was all that was needed. ( brettclapper.wordpress.com/2012/07/06/… )

    – M.C.

    Jul 25, 2017 at 11:06

  • 2

    Apart from all the answers, I would like to add one. From PHP 5.4.0 onwards, you can move to your project root using the command line and execute php -S <host>:<port>. For example php -S localhost:80.

    Sep 3, 2017 at 14:55


381

Ok, per pix0r‘s, Sparks‘ and Dave‘s answers it looks like there are three ways to do this:


Virtual Hosts

  1. Open C:\xampp\apache\conf\extra\httpd-vhosts.conf.
  2. Un-comment ~line 19 (NameVirtualHost *:80).
  3. Add your virtual host (~line 36):

    <VirtualHost *:80>
        DocumentRoot C:\Projects\transitCalculator\trunk
        ServerName transitcalculator.localhost
        <Directory C:\Projects\transitCalculator\trunk>
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
    
  4. Open your hosts file (C:\Windows\System32\drivers\etc\hosts).

  5. Add

    127.0.0.1 transitcalculator.localhost #transitCalculator
    

    to the end of the file (before the Spybot – Search & Destroy stuff if you have that installed).

  6. Save (You might have to save it to the desktop, change the permissions on the old hosts file (right click > properties), and copy the new one into the directory over the old one (or rename the old one) if you are using Vista and have trouble).
  7. Restart Apache.

Now you can access that directory by browsing to http://transitcalculator.localhost/.


Make an Alias

  1. Starting ~line 200 of your http.conf file, copy everything between <Directory "C:/xampp/htdocs"> and </Directory> (~line 232) and paste it immediately below with C:/xampp/htdocs replaced with your desired directory (in this case C:/Projects) to give your server the correct permissions for the new directory.

  2. Find the <IfModule alias_module></IfModule> section (~line 300) and add

    Alias /transitCalculator "C:/Projects/transitCalculator/trunk"
    

    (or whatever is relevant to your desires) below the Alias comment block, inside the module tags.


Change your document root

  1. Edit ~line 176 in C:\xampp\apache\conf\httpd.conf; change DocumentRoot "C:/xampp/htdocs" to #DocumentRoot "C:/Projects" (or whatever you want).

  2. Edit ~line 203 to match your new location (in this case C:/Projects).


Notes:

  • You have to use forward slashes “https://stackoverflow.com/” instead of back slashes “\”.
  • Don’t include the trailing “https://stackoverflow.com/” at the end.
  • restart your server.

12

  • 14

    On step (3) to edit the C:\Windows\System32\drivers\etc\hosts. Best practise to run editor (Notepad, Notepad++ or any editor) as Administrator first. So you can save directly to the C:\Windows\System32\drivers\etc\ folder.

    Dec 27, 2010 at 6:32

  • 3

    If you get 403 errors refer to this link

    Jan 20, 2014 at 10:47

  • 24

    Using Order allow,deny Allow from all on Win7 returns 403 ERROR because this options are deprecated. Use Require all granted. stackoverflow.com/questions/9110179/…

    – hywak

    May 17, 2014 at 12:30


  • 4

    Just FYI, this step didn’t appear to be necessary for me to do: Un-comment line 19 (NameVirtualHost *:80)

    Nov 8, 2014 at 1:20

  • 4

    Using Order allow,deny Allow from all didn’t work for me, but Require all granted worked.

    Mar 15, 2016 at 13:43

100

You can relocate it by editing the DocumentRoot setting in XAMPP\apache\conf\httpd.conf.

It should currently be:

C:/xampp/htdocs

Change it to:

C:/projects/transitCalculator/trunk

3

  • 41

    Don’t forget to edit it in BOTH lines that ask for DocumentRoot. If you only change the top one, you’ll get read-access errors etc.

    – Arcolye

    Dec 18, 2010 at 3:24

  • 2

    this apply for XAMP mac as well ?

    Jun 21, 2011 at 2:54

  • now by which URL, can access index page?

    Aug 5, 2016 at 14:34

52

A VirtualHost would also work for this and may work better for you as you can host several projects without the need for subdirectories. Here’s how you do it:

httpd.conf (or extra\httpd-vhosts.conf relative to httpd.conf. Trailing slashes “\” might cause it not to work):

NameVirtualHost *:80
# ...
<VirtualHost *:80>  
    DocumentRoot C:\projects\transitCalculator\trunk\
    ServerName transitcalculator.localhost
    <Directory C:\projects\transitCalculator\trunk\>  
        Order allow,deny  
        Allow from all  
    </Directory>
</VirtualHost> 

HOSTS file (c:\windows\system32\drivers\etc\hosts usually):

# localhost entries
127.0.0.1 localhost transitcalculator.localhost

Now restart XAMPP and you should be able to access http://transitcalculator.localhost/ and it will map straight to that directory.

This can be helpful if you’re trying to replicate a production environment where you’re developing a site that will sit on the root of a domain name. You can, for example, point to files with absolute paths that will carry over to the server:

<img src="https://stackoverflow.com/images/logo.png" alt="My Logo" />

whereas in an environment using aliases or subdirectories, you’d need keep track of exactly where the “images” directory was relative to the current file.

2

  • I have several network drives on my PC which have been mapped to drive letters (e.g. X:\myfolder). When I use this method I get an “Access Forbidden” warning. Does this mean the Apache user doesn’t have read access to the drive?

    – harryg

    May 20, 2013 at 14:43

  • 1

    Under Directory replace Order allow,deny and Allow from all with Require all granted. See stackoverflow.com/questions/9110179/…

    – Michiel

    Feb 3, 2016 at 10:11