Question

adding subdomain in apache virtual host

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.

What am I doing wrong?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@jop02

You are redirecting the traffic from http://api.example.com to https://api.example.com so you need to setup another virtual host for https://api.example.com where it listen on port 443 and uses a certificate for api.example.com.

Because you do not currently have this config file, it shows the default site example.com.

Hope this helps

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.