Score:0

Apache VirtualDocumentRoot multiple subdirectories possible

za flag

I have a local Apache server for developing pages. Therefore, I have a root folder where all the different pages are in subfolders. Those folders then have another subfolders for the public content. This could be - depending on the framework - a different folder (e.g. public, wwwroot, webroot, httpdocs).

Folder structure:
/
|-- srv
    |-- http
        |-- page
        |   |-- webroot
        |       |-- .htaccess
        |       |-- index.php
        |-- page2
            |-- public
                |-- index.php

Now instead of using http://localhost/page/webroot I want to use the http://page.localhost/ URL scheme because it should be like in real world (e.g. links which point to root should work and so on).

First I tried with VirtualDocumentRoot /srv/http/%-2/webroot/ which seems to work pretty fine if all pages have the webroot folder as public folder. However, it does not work if the public folder has a different name.

So my second approach was to use mod_rewrite. with something like:

DocumentRoot /srv/http
RewriteEngine On

RewriteCond %{HTTP_HOST} (.+)\.[^\.]+$
RewriteCond %{DOCUMENT_ROOT}/%1/webroot -d
RewriteRule ^(.+) %{DOCUMENT_ROOT}/%1/webroot/$1 [L]

(The last 3 lines can be repeated for the other folders - skipped here for readability)

That looked quite promising in the beginning, but then I found out that it does not work if the page has a .htaccess file with local mod_rewrite in it which maps all requests to index.php (like some framework do). In this case the mod_rewrite will run into an endless loop as the dynamic part of the path is treated like a subfolder and not like the DocumentRoot. Here is the output from the log: https://pastebin.com/4aNgRahx

This repeats until the recursion limit is reached.

The .htaccess file looks like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^ index.php [L]
</IfModule>

If I switch it off it will work.

So what I should do now?

Changing the .htaccess or any files in the project folder is not an option because it will not work anymore in the real server then.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.