是否可以配置xampp来提供htdocs目录之外的文件?

例如,假设我有一个文件的位置如下:

C: \ \ transitCalculator \树干\ TransitCalculator.php项目

我的xampp文件通常从:

C:\xampp\htdocs\

(因为这是默认配置)是否有一些方法让Apache识别和服务我的TransitCalculator.php文件,而不移动到htdocs下?最好我想Apache提供/有权访问项目目录的全部内容,我不想移动htdocs下的项目目录。

edit:编辑后将Apache添加到问题标题中,使Q/A更“可搜索”


您可以设置Apache为来自任何地方的页面提供服务,但它通常以更安全的形式分布。

编辑你的apache文件(http.conf是一个比较常见的名称)将允许你设置任何文件夹,使它出现在你的webroot。

编辑:

别名myapp c:\myapp\

我已经编辑了我的答案,包括在http.conf文件中创建别名的格式,这有点像windows中的快捷方式或un*x下的符号链接,Apache '假装'文件夹在webroot中。从长远来看,这可能对你更有用。

您可以通过编辑XAMPP\apache\conf\httpd.conf中的DocumentRoot设置来重新定位它。

目前应该是:

C:/xampp/htdocs

改为:

C: /项目/ transitCalculator /树干

好的,根据pix0r, Sparks和Dave的回答,看起来有三种方法可以做到这一点:


虚拟主机

Open C:\xampp\apache\conf\extra\httpd-vhosts.conf. Un-comment ~line 19 (NameVirtualHost *:80). 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> Open your hosts file (C:\Windows\System32\drivers\etc\hosts). 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.

现在您可以通过浏览http://transitcalculator.localhost/访问该目录。


制作别名

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.


更改文档根目录

在C:\xampp\apache\conf\httpd.conf中编辑~第176行;更改DocumentRoot“C:/xampp/htdocs”为#DocumentRoot“C:/Projects”(或任何你想要的)。 编辑~ 203行以匹配新位置(在本例中是C:/Projects)。


注:

你必须使用前斜杠“/”而不是后斜杠“\”。 不要包括结尾的“/”。 重新启动服务器。

VirtualHost也可以解决这个问题,而且可能更适合你,因为你可以在不需要子目录的情况下托管多个项目。你可以这样做:

Httpd.conf(或者额外的\httpd-vhosts.conf相对于Httpd.conf。后面的斜杠“\”可能会导致它不起作用):

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文件(c:\windows\system32\drivers\etc\ HOSTS通常):

# localhost entries
127.0.0.1 localhost transitcalculator.localhost

现在重新启动XAMPP,您应该能够访问http://transitcalculator.localhost/,它将直接映射到该目录。

如果您正在尝试复制一个生产环境,在该环境中您正在开发一个将位于域名根目录上的站点,这可能会很有帮助。例如,你可以指向带有绝对路径的文件,这些文件将被传输到服务器:

<img src="/images/logo.png" alt="My Logo" />

然而,在使用别名或子目录的环境中,您需要准确跟踪“images”目录相对于当前文件的位置。

解决方案:允许Apache 2在htdocs之外托管网站:

在httpd.conf中的“DocumentRoot”指令下面,你应该看到一个目录块。将这个目录块替换为:

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

请记住不要在实际环境中使用此配置

如果你试图让XAMPP使用网络驱动器作为你的文档根,你必须在httpd.conf中使用UNC路径。XAMPP将无法识别映射的网络驱动器。

例如,下面的方法行不通, DocumentRoot“X: / webroot”

但是这个会, DocumentRoot "//192.168.10.100/webroot"(注意前面的斜杠,而不是后面的斜杠)