Search code examples
apache.htaccessmod-rewritemamp

Configure MAMP to treat each directory in Htdocs as Root in Apache mod_redirect


The issue is that when using MAMP for local development, the .htaccess file needs to differ in that the RewriteBase needs to specify the subdirectory for the specific site (as shown here on SO).

Is there a way to configure MAMP MAMP/conf/apachehttpd.conf so that each virtual site gets it's own "root"?

UPDATE

After getting a clue about Virtual Hosts:

Have updated /etc/hosts file to include:

127.0.0.1       ClientSite.localhost

Uncommented the line:

`#Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf` 

in /Applications/MAMP/conf/apache/httpd.conf.

There is a directory called ClientSite in /Users/myname/Sites/.

This is the /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf content:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    ServerName localhost
    ServerAlias *.localhost
    VirtualDocumentRoot /Users/myname/Sites/%0

    RewriteLogLevel 3
    RewriteLog "/Applications/MAMP/logs/rewrite.log"

    <Directory /Users/myname/Sites>
       Options All
       AllowOverride All
       Order allow,deny
       Allow from all
    </Directory>
</VirtualHost>

Restarted the MAMP servers (and even ran dscacheutil -flushcache).

When browser is pointed to ClientSite.localhost it returns a 404: The requested URL / was not found on this server.


Solution

  • so that each virtual site gets it's own "root"?

    You need to use VirtualDocumentRoot.

    This is how I am using this on my MAMP in my /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf file:

    <VirtualHost *:80>
        ServerAdmin webmaster@dummy-host.example.com
        ServerName localhost
        ServerAlias *.localhost
        VirtualDocumentRoot /Users/admin/htdocs/%0
    
        RewriteLogLevel 3
        RewriteLog "/Applications/MAMP/logs/rewrite.log"
    
        <Directory /Users/admin/htdocs>
           Options All
           AllowOverride All
           Order allow,deny
           Allow from all
        </Directory>
    </VirtualHost>
    

    Take note of VirtualDocumentRoot /Users/admin/htdocs/%0 directive. That makes each virtual site's root as:

    VirtualDocumentRoot /Users/admin/htdocs/localhost
    VirtualDocumentRoot /Users/admin/htdocs/dev.localhost
    VirtualDocumentRoot /Users/admin/htdocs/client2.localhost
    

    etc.

    Then simply create a directory within /Users/admin/htdocs/ for each site named as above, like:

    dev.localhost
    client2.localhost
    

    Remove (or rename) any .htaccess files during the process - and once websites confirmed to be accessible via url like: http://client2.localhost, .htaccess files should behave as expected.

    Also be sure that in the /etc/hosts file, there's an entry like:

    127.0.0.1    client2.localhost
    

    for each URL in question.