Make XAMPP/Apache serve file outside of htdocs

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:projectstransitCalculatortrunkTransitCalculator.php

and my xampp files are normally served out from:

C:xampphtdocs

(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"


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


Virtual Hosts

  • Open C:xamppapacheconfextrahttpd-vhosts.conf.
  • Un-comment line 19 ( NameVirtualHost *:80 ).
  • Add your virtual host (~line 36):

    <VirtualHost *:80>
        DocumentRoot C:ProjectstransitCalculatortrunk
        ServerName transitcalculator.localhost
        <Directory C:ProjectstransitCalculatortrunk>
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
    
  • Open your hosts file (C:WindowsSystem32driversetchosts).

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

  • 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).
  • Restart Apache.
  • Now you can access that directory by browsing to http://transitcalculator.localhost/.


    Make an Alias

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

  • 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

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

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

  • You can relocate it by editing the DocumentRoot setting in XAMPPapacheconfhttpd.conf.

    It should currently be:

    C:/xampp/htdocs

    Change it to:

    C:/projects/transitCalculator/trunk


    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 extrahttpd-vhosts.conf relative to httpd.conf. Trailing slashes "" might cause it not to work):

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

    HOSTS file (c:windowssystem32driversetchosts 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.

    链接地址: http://www.djcxy.com/p/41288.html

    上一篇: 使用Django开发服务器和Apache在浏览器中显示图像

    下一篇: 使XAMPP / Apache服务于htdocs之外的文件