3

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

pjk_ok
  • 618
  • 7
  • 35
  • 90
  • You usually tell the server (Apache, Nginx, IIS, etc.) where the site's "root" is, and that is where all of your code _starts_ from. That code can use `include` and `require` to access additional PHP code elsewhere, and it can read the file system of other folders (assuming server permissions are correct). This is all usually done so that you can separate files from being directly accessible in the URL, be it PHP classes (like things from Composter) or log files or custom uploads. I don't know your specific case for public/private, but it is a little strange to see the index.php file outside – Chris Haas May 10 '21 at 22:39
  • @ChrisHaas I do have index.php files in the public and private folders too. I thought you should always have an index.php file in every folder for security reasons? because I'm using MAMP I guess what I'm looking for then is the Apache code that sets the public folder as the root so when I upload the site to a live server this will still work as it does on the localhost? – pjk_ok May 11 '21 at 02:18
  • The "put a index.php in every folder" is to me an older style of thinking and often related to shared or managed hosting. Not right or wrong, but it was a guard against a misconfigured server, and to me, I'd just make sure that my servers were configured correctly. (Also, I host on Nginx where it is more common to have absolute control of configuring your server anyway.) – Chris Haas May 11 '21 at 13:58
  • Could you please check this - https://stackoverflow.com/questions/23635746/htaccess-redirect-from-site-root-to-public-folder-hiding-public-in-url – Dmitry Leiko May 12 '21 at 07:28

1 Answers1

0

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

Haridarshan
  • 1,898
  • 1
  • 23
  • 38