1

I'm trying to make it so when someone visits my site at sub.example.com, it sends them to a different folder than someone who visits example.com. Simple, yes? This is the code I use to do it:

NameVirtualHost *:80
<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/sub"
    ServerName sub.example.com
    ServerAlias sub.example.com
</VirtualHost>

I have the DNS for my domain set for a wildcard so that any subdomain still directs to my server.

The problem is, now whenever someone visits example.com or sub.example.com or any subdomain, it shows the content from /htdocs/sub

I only want it to show /htdocs/sub when sub.example.com is visited, how do I do that?

From what I've read my execution seems right, but maybe I've missed one thing that causes this to happen? I've reinstalled xampp to no avail

My main httpd.conf has this line ServerName example.com:80 if this helps

Josh
  • 21
  • 3

1 Answers1

1

I ended up finding a solution, and to anyone in the future with the same problem, this is what I did:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot "C:/xampp/htdocs/"
</VirtualHost>


<VirtualHost *:80>
    ServerName sub.example.com
    DocumentRoot "C:/xampp/htdocs/sub"
</VirtualHost>

I assume that basically for the VirtualHost to differentiate between sub.example.com and example.com then a VirtualHost first must be defined for example.com before any subdomains can be configured

Josh
  • 21
  • 3
  • Sounds like you already found your own answer, but here's a link to a previous post that explains the differences between redirects/rewrites/vhosts: http://stackoverflow.com/questions/30602324/how-to-detect-an-incoming-request-with-php-script-from-a-cname-subdomain/30630937#30630937 – Rick Buford Jul 20 '15 at 22:24