Set Root Folder To SubFolder In Apache On LocalHost That Will Also Work On Production Server

Viewed 992

I have a project that has a 'public' and 'private' folder structure that sit at the same level inside the main project root folder (see attached image).

I would like so that the local server (in this case MAMP) automatically defaults to the 'public' folder as the domain root. Is it best to do this is in php or in Apache?

Also, when I upload the site to a production server the domain name is going to be set on the 'public' folder, so I'd ideally like a solution that means I when I upload the site this will still be the case - i.e. www.example.com will be the 'project/public' folder.

This is all new to me so am at a bit of loss as to the best way to approach this.

folder structure

1 Answers

Basically all you need is to create a virtual host configuration file and enable the port in Apache configuration file on which you're application is going to listen to the requests. I've never used MAMP so am giving you an example of vhost configuration on Linux machine

  1. Create a conf file in /etc/httpd/conf.d/

    vim /etc/httpd/conf.d/example.conf

  2. This file will tell Apache server what will be the document root for a specific port. PORT can be any available port other then reserved Ports (0-1023)

    <VirtualHost *:80> KeepAlive On

    DocumentRoot "/var/www/example/public"
    <Directory "/var/www/example/public/">
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    
        # enable mod_rewrite
        RewriteEngine on
    
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.+)/$
        RewriteRule ^ %1 [L,R=301]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </Directory>
    
    LogLevel Warn       
    
    ErrorLog  /var/log/httpd/example-error.log
    CustomLog /var/log/httpd/example-access.log combined
    
  3. Add this Port No PORT in Apache conf file (/etc/httpd/conf/httpd.conf)

    Listen PORT

  4. Restart Apache Server

Like XAMP & WAMP, MAMP should also have a GUI to configure the Apache Server. You can locate the location/folder where these vhost configuration are save and you can modify that file directly through any Text Editor

You can refer these too

Configuring Virtual Hosts With MAMP

Adding a virtual host in MAMP for Mac

Related