0

I am trying to configure a subdomain: api.example.com (exapmle.com is replaced by my actual domain name)

I already had virtual hosts set up with 2 sites: example.com and example.nl they both had separate directories and where working correctly as separate sites.

Now I want to add api.example.com also with its own separate documentroot. I added a config file called: "api.example.com.conf" with this inside:

<VirtualHost *:80>
ServerAdmin admin@api.example.com
ServerName api.example.com
ServerAlias www.api.example.com
DocumentRoot /var/www/api.example.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.api.example.com [OR]
RewriteCond %{SERVER_NAME} =api.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

I added the folder /var/www/api.example.com/html with an example index.html inside. I gave the folder the right permissions. After that I enabled the site with: "a2ensite api.example.com.conf" and restarted apache.

I added a new record to the example.com records. It is an A record with api.example.com and it redirects to the same ip as example.com because they are on the same server.

After doing all of this I expected that when I went to api.example.com that it would show me the example index.html that I had created but it did not. When I go to api.example.com it is the same thing as if I would go to example.com.

How do I make it so that api.example.com has its own document root that works correctly?

  • Did you reload Apache after adding this configuration? – Daniel B Jan 13 at 14:38
  • Yes, I did reload apache, – C1rcu1tB04rd Jan 13 at 14:55
  • Have you tried omitting the Rewrite rules? – Anaksunaman Jan 13 at 17:06
  • Yes, I have and I did not work. – C1rcu1tB04rd Jan 13 at 17:08
0

I am forcing all my domains to https but I did not yet have SSL setup for my new subdomain. I did have a virtual host for http://api.example.com, but my server does not allow HTTP so it redirected to https and because there was no virtual host for https://api.example.com it just redirected to the main domain.

  • Hi C1rcu1tB04rd, the answer section is intended for answers only please edit your question or use comments to provide supplements and delete this one. – bummi Jan 13 at 18:14
  • But that’s not a question update, it’s a proper answer. “There was no vhost api.example.com on *:443”. – Daniel B Jan 13 at 19:35

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.