1

I have a single app, which shares a large amount of code in PHP but which has 3 separate application-level folders depending on the domain name entered. IE:

  • app.example.com is /apps/app-name/frontend
  • portal.example.com is /apps/app-name/portal
  • api.example.com is /apps/app-name/api

How do I customize Apache, either through .htaccess or through Heroku settings so that connections to app.example.com are directed to the frontend directory?

I have thought about doing this via a master index file which imports the correct app from its folder using an include statement based on domain but this seems fairly hacky to me and I would rather do this at the server config level that from within the app. Have looked all over and have been unable to find any useful information.

I use Heroku for hosting by default which only allows one web process type and which associates to a given site root.

0

Apache has functionality for this built in. You need to use three VirtualHost configuration sections. See the Apache documentation for more details and examples.

<VirtualHost *:80>
    DocumentRoot "/apps/app-name/frontend"
    ServerName app.example.com
    # Other directives here
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/apps/app-name/portal"
    ServerName portal.example.com
    # Other directives here
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/apps/app-name/api"
    ServerName api.example.com
    # Other directives here
</VirtualHost>

These settings need to go in the apache.conf file or directory. It is usually located in the /etc/ directory like at /etc/apache2/apache.conf or /etc/httpd/httpd.conf. You should read about how to configure virtual hosts on your particular operating system. Different distributions expect it to be set up differently. For example on Debian/Ubuntu, you should create three new files like /etc/apache2/sites-available/api.example.com.conf then use the command sudo a2ensite api.example.com to enable each. You also need to restart or reload apache after making conf changes.

  • Sorry Stephen, forgot to add that I use Heroku so config is done through htaccess files and the Heroku procfile. – Chris Rutherfurd Mar 9 at 11:50
  • I don't know anything about Heroku, I've never used it. The apache.conf takes precedence over .htaccess, so it is fine to use it and .htaccess. At most you would have to duplicate .htacess files between those three directories. – Stephen Ostermiller Mar 9 at 12:05
  • Is heroku a hosting company it a framework? – Stephen Ostermiller Mar 9 at 13:30
  • It's a hosting company that provides container based hosting for web apps. You define the service by defining the web root and Apache is automatically configured. – Chris Rutherfurd Mar 20 at 10:22
  • Then this answer isn't going to be helpful if you don't have the ability to create virtual hosts. – Stephen Ostermiller Mar 20 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.