Jump to content

Is it possible to "redirect" htdocs to another folder?


Recommended Posts

I installed Xampp on my Windows computer. It actually works well, but one thing that is a bit annoying is the fact that when you want to show the output of a PHP document, then it is necessary for the site to be located in the htdocs folder of C:\xampp. Or is it necessary? Is there a way to sort of "redirect" to another folder on the computer? I have my system (Windows 7) on my C drive and my data on my D drive. Sometimes I clone my C drive to keep it up to date and fast. Is there a way to have my website files on the D drive and Xampp installed in the default location on C:\xampp and still be able to test my site locally?

 

Erik 

Link to comment
Share on other sites

Yes it is possible and exactly the same thing I did.  You need to edit a line in the httpd.conf file and look for lines like this

# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "d:"

And this

# This should be changed to whatever you set DocumentRoot to.
#
<Directory "d:">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #

And just change the default c: to d:

Link to comment
Share on other sites

Thanks fastsol. I will consider using it. At the moment I am however not sure which way to do it. My situation is, that I have two websites operated on the same computer, and I would like to avoid having to switch the settings in the httpd.conf file whenever I change working from one website to the other. At the moment my local mirrorpages (with subdirectories) are located side by side: D:\site1 and D:\site2.

 

I found the webpage trq linked to directly at Apache.org difficult to follow. I found another page discussing the issue. Maybe they have a better way to do it? I will appreciate any comments on this.

 

http://stackoverflow.com/questions/1408/make-xampp-apache-serve-file-outside-of-htdocs

 

Regards,

 

Erik

Link to comment
Share on other sites

 

 

 found the webpage trq linked to directly at Apache.org difficult to follow. I found another page discussing the issue. 

That is the same as what trq linked to. For what you are wanitng to do you are going to need to setup virtualhosts for each project/site. This is only way you can host multiple sites from one server instance

 

Setting up virtual hosts looks complicated at first, but in actual fact it is rather simple

 

For each  <virtualhost></virtualhost> directive you need to define a  ServerName  this is the domain name for the site (eg site1.localhost, site2.localhost).  The other important directive each virtualhost needs is a  DocumentRoot . This defines where the sites files are located for the site.

 

Your very basic virtualhost config would be some like

<VirtualHost *:80>
    ServerName site1.localhost   # domain for site1

    DocumentRoot "D:/site1"      # document root for site1

    <Directory "D:/site1">       # the directory config options
       Options Indexes FollowSymLinks
       AllowOverride All
       Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName site2.localhost   # domain for site2

    DocumentRoot "D:/site2"      # document root for site2

    <Directory "D:/site2">       # the directory config options
       AllowOverride All
       Allow from all
    </Directory>
</VirtualHost>

Now add the following to your Windows hosts file (located in C:\Windows\System32\drivers\etc\).

127.0.0.1 site1.localhost
127.0.0.1 site2.localhost

Restart Apache you should now be able to access http://site1.localhost and http://site2.localhost which should serve the sites located in D:/site1 and D:/site2 respectively. You can add as many virtualhosts as you like. 

Edited by Ch0cu3r
Link to comment
Share on other sites

I tested the option of just changing the DocumentRoot in the httpd.conf file as suggested by fastsol and figured it to work right away. It is absolutely an option, because it is so simple.

 

Regarding creating a VirtualHost, or even several Virtualhosts, it looks more elegant in the way that one doesn't need to include subdirectories in the URL when watching pages locally in the browser. Unfortunately the procedure to make the VirtualHosts work is not so simple. I tested your code thoroughly, Ch0cu3r, but it didn't work! The link to Apache.org delivered by trq didn't bring me much further to the goal. I don't like the style in Apache.org in general. Often it only states how things should be done, and not WHY. I need explanations in order to understand the logic behind the procedure. At least the explanations at Apache.org are very sparse. I find that if one doesn't understand the structure beforehand, one won't learn anything from looking into the Apache.org documentation. So I went on Googling for other explanations. I found the following pages to be helpful, delivering much better explanations:

 

http://foundationphp.com/tutorials/apache_vhosts.php

 

http://www.apptools.com/phptools/virtualhost.php

 

In general different pages deliver different procedures on how to create VirtualHosts. It seems somewhat shaky in the sense that if you don't do it in the exact right way, it won't work, and it is difficult to find out what is wrong. For example one should be aware of opening the different files as administrator by rightclicking on Notepad in the Start menu. Secondly the procedure to make VirtualHosts work seems to have been changed in recent versions of Apache. That's at least what I conclude after watching several webpages delivering procedures, which don't work anymore. Anyway, after having used several hours of this subject I finally made it work. Here is how I did:

 

I have two sites named site1 and site2 located at D:\site1 and D:\site2. I decided to name the VirtualHosts site1.local and site2.local as suggested by one of the pages above. Then I made changes to three different files. Two of them located in the Xampp directory (I have Xampp installed) and the third is a Windows system file:

 

C:\xampp\apache\conf\httpd.conf

C:\xampp\apache\conf\extra\httpd-vhosts.conf

C:\Windows\System32\drivers\etc\hosts

 

Make sure to have Windows Explorer set to show all files (also system files), or you won't see anything. Also after having opened Notepad as Administrator by rightclicking on it in the Start menu, one also needs to look for all files *.*, not just text files as the default *.txt. Again if you don't, you can't see the files. Now to the changes:

 

In the httpd.conf file, I made sure that the comment-sign # in front of the below Include line was removed. I think it is already removed in the newer versions of Apache, but apparently wasn't in earlier versions:

Include conf/extra/httpd-vhosts.conf

In the hosts file in the Windows system, I added two lines at the end of the file: 

127.0.0.1 site1.local
127.0.0.1 site2.local

The httpd-vhosts.conf file need the most changes. At the bottom you should place the VirtualHosts commands, in which the DocumentRoot and ServerName for each VirtualHost is listed. Notice that it is a good idea also to make a VirtualHost to the localhost. If you don't, you won't be able to use localhost, and maybe more severe: You won't be able to use your local phpMyAdmin! Higher on your page you need to write a Directory command. If you don't, you won't be able to locally access subdirectories via the browser. Finally you should remove the # sign in front of the NameVirtualHost *:80 command already in the file. 

...

<Directory D:>
    Require all granted
</Directory>

...

# The line below is originally commented out. The #-sign should be removed to make it take action:
NameVirtualHost *:80

...

<VirtualHost *:80>
   DocumentRoot C:/xampp/htdocs
   ServerName localhost
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot D:/site1
   ServerName site1.local
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot D:/site2
   ServerName site2.local
</VirtualHost>

Finally when you have made the changes in the three files and having saved the files, it is important that you close and open the Apache server, before you can see the changes having taken action. You can close and open the Apache server in the Xampp Control Panel by hitting a button on the line with the Apache server. And then start it again by hitting it again. Now hopefully things should work for you. You can test it by writing the following URL in a browser:

 

http://site1.local

 

assuming you have an index.html file in the root of your site (else add the filename to the URL:  http://site1.local/somefile.php

 

Questions: I am not sure why it is necessary and even if it is necessary to uncomment the line NameVirtualHost *:80 in the httpd-vhosts.conf file. If someone know, I would like to hear it. Secondly I am not sure if the "Require all granted" line in the Directory command is the right one to use. Some other websites deliver other commands, but it can have to do with old versions of Apache. I am not sure. I guess it has security questions also. I will appreciate any comments on that too. 

 

At least my Virtual Hosts work now :)

 

Regards,

 

Erik

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.