views:

11532

answers:

9

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

For instance, say I have a file:

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

and my xampp files are normally served out of:

C:\xampp\htdocs\

(because that's how it's configured by default) 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"

+2  A: 

You can set Apache to serve pages from anywhere with any restrictions but it's normally distributed in a more secure form.

Editing your apache files (http.conf is one of the more common names) will allow you to set any folder so it appears in your webroot.

EDIT:

alias myapp c:\myapp\

I've edited my answer to include the format for creating an alias in the http.conf file which is sort of like a shortcut in windows or a symlink under un*x where Apache 'pretends' a folder is in the webroot. This is probably going to be more useful to you in the long term.

sparkes
+5  A: 

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

Dave Ward
+13  A: 

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>

  1. Open your hosts file (C:\Windows\System32\drivers\etc\hosts)
  2. 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)
  3. 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)
  4. Restart Apache

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


Make an Alias

  1. Starting ~line 200, copy everything between and (~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 section (~line 300) and add Alias /transitCalculator C:/Projects/transitCalculator/trunk (or whatever is relevant to your desires) below the "Alias" comment block.


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 "/" instead of back slashes "\"
  • don't include the trailing "/" at the end
  • restart your server.
cmcculloh
+6  A: 

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="/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.

pix0r
This is a really elegant answer man, thanks.
Mladen Mihajlovic
A: 

None of these solutions worked easily. Here is the simplest solution to allow Apache 2 to host websites outside of htdocs:

Underneath the "DocumentRoot" directive in httpd.conf, you should see a directory block. Replace this directory block with:

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Allow from all
</Directory>

REMEMBER NOT TO USE THIS CONFIGURATION IN A REAL ENVIRONMENT

A: 

I've tried all the aforementioned methods and none of them worked. No matter what path I specify in the , it still redirects to the original DocumentRoot.

A: 

For vhosts to work I had to enable the include file on line 471 of your http.conf file. After that the directions above worked.

Mike Dobbs
A: 

hi,

I have data in N drive and I wana make Alias.

I opened this file "C:\xampp\apache\conf\httpd.conf"

in the end of httpd.conf I wrote these lines

Alias /portal/ "/engmov/"
<Directory "N:/ENGLISH MOVIES/">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>

when I browse this link "http://localhost/engmov/" page opens with "Object not found" "Error 404"

I'll be very thank full if anybody helps me

Regards

Naeem

Naeem
This should be posted as a new question rather than an answer to a posted question.
cmcculloh
A: 

I think you made an error, the alias have to be between < IfModule alias_module > ... < /IfModule >: Alias /engmov "N:/ENGLISH MOVIES/"

And you need to write the full path.

Kind regars, Wajdi.

wajdi