0

Sorry for asking this question again. Even though this has been asked and discussed quite a bit I cannot seem to find the right solution for a local dev environment using VirtualHost. I am using XAMPP Portable for Windows for dev work but assume this is the same for any other local server with regards to the .htaccess file.

DocumentRoot of VirtualHost is D:\dev\www\ for example.

ServerName is devwork.webdev for example.

HOSTS entry is 127.0.0.1 devwork.webdev.

VirtualHost file does have a default DocumentRoot being DocumentRoot "D:/xampp/htdocs". It works just fine.

The projects are each in a folder under D:\dev\www\ for example D:\dev\www\project01\ or D:\dev\www\project02\ and so on and show nicely in the browser when going to devwork.webdev with Options Indexes FollowSymLinks enabled. Apache is not showing any error and the access log file is also OK, things are working.

Now in my HTML when I use <a href="/" class="navbar-brand">Project 01</a> a click on the link does link me to D:\dev\www\ showing all the projects I have in that folder.

Instead I would like to be linked to the root of the project, being D:\dev\www\project01\ or rather http://devwork.webdev/project01/.

How can I get that to work?

I am looking for a solution to this so that I can do dev work locally and without changing the HTML later FTP the data to the live host's root and it will work.

I have read and tried the following:
http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
https://perishablepress.com/redirect-subdirectory-to-root-via-htaccess/ .htaccess How to redirect root URL to subdirectory files, rewrite to clean URL AND not affect subdomains?
http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
https://stackoverflow.com/a/990405/1010918
How to redirect /directory/index.html and /directory/index.php to /directory/
Redirecting /directory/index.html to /directory/
with
How to remove .html from URL
and
http://forums.modx.com/thread/77211/endless-friendly-url-redirect-from-subdomain-folder-location
being closest to what I think I need but I must be doing something wrong since I always arrive at the DocumentRoot instead of the folder where the project is kept under the DocumentRoot.

Thank you for any help.

Community
  • 1
  • 1
lowtechsun
  • 1,915
  • 5
  • 27
  • 55
  • Why don't you just set DocumentRoot to `D:\dev\www\project01`? – Alois Mahdal Sep 28 '15 at 17:58
  • 1
    Also it seems you are misunderstading VirtualHost. VirtualHost is for cases if you want to imitate several different hosts on a single machine. E. g. if you create a VirtualHost setup for `project01.devwork.webdev`, add that hostname into etc/hosts, and set its DocumentRoot to `D:\dev\www\project01`, all requests to `http://project01.devwork.webdev/` should open project01 (but not the list of project) – Alois Mahdal Sep 28 '15 at 18:02
  • Will try what you suggested, though I am not setting up subdomains for every VirtualHost/Project (and then add those to the HOSTS file each time) I have. I keep them all in folders under the VirtualHost DocumentRoot like described in the question. You see my DocumentRoot is `D:\dev\www` and each project is in a subfolder of `WWW`. Otherwise I would have to setup a VirtualHost and edit the HOSTS file each time I do a quick test on a project. Simply creating a folder in the VirtualHost DocumentRoot and linking to that folder via `"/"` in HTML is what I am after. – lowtechsun Sep 28 '15 at 18:33
  • Of course you can keep it all on single host and just access the subfolders, but that way your requirement of not having to change HTML can't be fulfilled: `/` can mean only one thing on your host: either `D:\dev\www` or somethig else. – Alois Mahdal Sep 28 '15 at 18:36
  • So wanting to be on a single host would me I would have to write `index.html` instead of `/`. Is this acceptable still and or good practice? Having `index.html` instead of `/` in the source? Kind of trying to write as concise as possible HTML and be able to quickly throw it on a live server without the hassle of changing all the links to the root of the project folder. – lowtechsun Sep 28 '15 at 18:50
  • I don't think this has anything to do with `/` vs. `index.html`. – Alois Mahdal Sep 28 '15 at 18:52
  • Well it does since you say on a single host it obviously redirects to the DocRoot and since the projects are in subfolders of the DocRoot I will have to use `index.html` instead of the shorter `"/"`. I was looking for a solution to link `"/"` in the HTML to the project folder somehow with a particular `.htaccess` rule, but you say this is not possible, right? – lowtechsun Sep 28 '15 at 19:37

2 Answers2

2

This seems closest:

  1. Add VirtualHost definition for each project you want to access. (I'm not sure how to do it on XAMPP for Windows).

    For example, project01.devwork.webdev...

  2. Set DocumentRoot for this VirtualHost to D:\dev\www\project01...

  3. Add the hostname to your /etc/hosts/ file.

  4. Open http://project01.devwork.webdev/ in your browser.

    You should see the application in D:\dev\www\project01, while all URLs will be based on "/".

What happens in the background:

When you open the URL http://project01.devwork.webdev/ in your browser, it will (as usual) translate it to IP address, but along with the request, it will also send Host header with the entered hostname:

GET / HTTP/1.1
Host: project01.devwork.webdev

Based on the Host field, Apache will decide which VirtualHost it needs to "pretend" to be, and serve files from the respective direcory.

However, if you need to have index of the projects, you will have to create it manually with full URLs.

Alois Mahdal
  • 10,763
  • 7
  • 51
  • 69
  • Can all this also be done without creating a VirtualHost definition and subdomain for each project and simply redirect to the respective's project folder? What is better with respect to ease of uploading the whole folder to the host's live server without changing the HTML at all? I am fine with having the .htaccess file locally while I do dev work on the project and when done simply upload the folder with the data to the host's live server - done. Would the whole VirtualHost (subdomain) for each project procedure enable me to do that? How is this done with purely subfolders of the DocRoot? – lowtechsun Sep 28 '15 at 18:35
  • @lowtechsun You don't redirect to folder, you redirect to another URL. It's up to you how you design the URL and where it points. Think about it: you want `/` to mean project01, you don't need redirection; you can simply set global DocumentRoot to project01 and be done, but that way you lose access to other project. There's no way for the server to magically know where you want to redirect. If you want to retain access to all projects, you *have* to have something project-specific in the URL (in my answer it's the host part). – Alois Mahdal Sep 28 '15 at 18:47
  • I get the logic. Yes makes sense. Do you know of any other way to do this, keep the projects folders under one host and still be able to use `"/"`? I have looked in a lot of places and found nothing and I think that is due to what you explained. If you know of a workaround or another solution I am all ears. I guess in the long run setting up a new VirtualHost for each project takes a tiny bit more time but it is much better than not having `"/"` in the source. What is better actually? To have `index.html` or to have `"/"` in the HTML source? Why? – lowtechsun Sep 28 '15 at 19:46
  • 1
    Forget the last part. I get the logic. See my edit. Thank you for your help. Not only did I think of VirtualHost in a wrong way and learned something now from you, I am now also able to use `"/"` in the HTML and move it to the live server without changing the HTML. Two big steps. Thank you for your guidance. Much appreciated! – lowtechsun Sep 28 '15 at 20:30
0

An alternative with the given logic would be to simply add a VirtualHost for each project in D:dev\www\projectname by giving it its own domain instead of a subdomain on the host.

There is little difference there now when already editing httpd-vhosts.conf (using XAMP this file is in InstallDir and then in apache\conf\extra )but ideally it makes copying the local HTML to the live server without changing the HTML possible.

So this is something devs doing local work should keep in mind. I certainly will!! Thank you for your help and information.

<VirtualHost *:80>
    DocumentRoot "D:/dev/www/newprojectname"
    ServerName newdomainname.webdev
    ServerAlias www.newdomainname.webdev

    ErrorLog "D:/dev/www/log/dev-apache.error.log"
    CustomLog "D:/dev/www/log/dev-apache.access.log" common   

    <Directory "D:/dev/www/newprojectname">

        AllowOverride All

        Options Indexes FollowSymLinks

        Require local
        # more detailed local
        # Require ip 192.168.188
        # or the IP from your local network

    </Directory>
</VirtualHost>
lowtechsun
  • 1,915
  • 5
  • 27
  • 55