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.


1 Answer 1


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 *:80>
    DocumentRoot "/apps/app-name/portal"
    ServerName portal.example.com
    # Other directives here
<VirtualHost *:80>
    DocumentRoot "/apps/app-name/api"
    ServerName api.example.com
    # Other directives here

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.

If all you have access to is .htaccess, you could use mod_rewrite to rewrite requests for these domains to subdirectories (source). The biggest problem with this approach is that it is not going to prevent the subdirectories from getting accessed from your main domain.

RewriteEngine on

RewriteCond %{HTTP_HOST} ^app.example.com$
RewriteCond %{REQUEST_URI} !frontend/
RewriteRule (.*) /frontend/$1 [L]

RewriteCond %{HTTP_HOST} ^portal.example.com$
RewriteCond %{REQUEST_URI} !portal/
RewriteRule (.*) /portal/$1 [L]

RewriteCond %{HTTP_HOST} ^api.example.com$
RewriteCond %{REQUEST_URI} !api/
RewriteRule (.*) /api/$1 [L]
  • Sorry Stephen, forgot to add that I use Heroku so config is done through htaccess files and the Heroku procfile. Mar 9, 2019 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. Mar 9, 2019 at 12:05
  • Is heroku a hosting company it a framework? Mar 9, 2019 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. Mar 20, 2019 at 10:22
  • Then this answer isn't going to be helpful if you don't have the ability to create virtual hosts. Mar 20, 2019 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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