1

I run MAMP on my mac book and have many projects. I use a single httpd-vhosts.conf for all my vhost configs. In one of my apps I need to have a 3rd party API redirect to my app at 127.0.0.1/[route]. My vhosts file is mapping the first project to 127.0.0.1 which isn't necessarily the project I want to redirect to.

I would like to be able to access any project from localhost or 127.0.0.1 suffixed by a project name, but it only maps to the first project in the vhost file. So the temporary solution is to comment out all the projects except the one I want to use at the current time and restart apache; not a great solution.

So I guess my question is how can I enable multiple projects to use 127.0.0.1/project_name or similar.

Jared Eitnier
  • 139
  • 1
  • 6
  • Not entirely sure if it would work, but since you are dealing with localhost/127.0.0.1 you could possibly add aliases via local disk/windows/system32/drivers/etc and change the hosts file by adding an alias. Not sure if you can do multiple. – Carl Carlson Feb 06 '14 at 16:45
  • If you are using a DNS server you can add redirects, but I'm not sure if you would be able to use localhost/127.0.0.1. – Carl Carlson Feb 06 '14 at 16:45
  • @CarlCarlson, I'm on a MacBook Pro – Jared Eitnier Feb 06 '14 at 16:52
  • On a mac there is generally a hosts file as well under an etc directory. Not sure what the directory path is leading up to the etc directory, but I imagine it is on the same level as a user directory. – Carl Carlson Feb 06 '14 at 21:27

1 Answers1

1

You have a couple of options here:

Option 1) Set up a single vhost document root with all of your projects underneath. So you would set up /Users/jared/projects/ and then have a dir under that for each of your projects ( /Users/jared/projects/project-a, /Users/jared/projects/project-b, etc ). Then in apache you would create one vhost like this:

<VirtualHost 127.0.0.1:80>
    ServerName localhost
    DocumentRoot /Users/jared/projects
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /Users/jared/projects/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

Any specific project settings would have to be done via .htaccess files in each project dir. This is the least ongoing maintenance type of setup but can have drawbacks.

Option 2) Set up different vhosts per project and use your /etc/hosts file to set domain names. For this you would have a file structure that looks like the example above and then in your /etc/hosts file you would add these lines:

127.0.0.1 project-a
127.0.0.1 project-b

Then in your vhost config you would have this:

<VirtualHost 127.0.0.1:80>
    ServerName project-a
    DocumentRoot /Users/jared/projects/project-a
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /Users/jared/projects/project-a/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

<VirtualHost 127.0.0.1:80>
    ServerName project-b
    DocumentRoot /Users/jared/projects/project-b
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /Users/jared/projects/project-b/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

This setup would allow you to have the various configs in each vhost block as well as adding unique logging per vhost as well. The downside of this is that for each new project you will need to do the setup in /etc/hosts and in the vhost config.

meatflag
  • 166
  • 3