-1

What I want to do seems easy enough to me in concept, but I've spent a little over 12 hours stuck on this with no luck. I think I'm close, but not sure. This is what I need:

  • example.com redirects to www.example.com (301)

  • www.example.com loads /home/example/www/example.com/public_html/

  • *.example.com loads /home/example/www/*example.com/public_html/ (THE WILDCARD)

This where am at... the wildcard subdomains aren't working, but the rest seems to, Any advice? I've seen tons of related posts, but nothing that has been able to get me up and running:

<VirtualHost 1.2.3.4:80>
   ServerName example.com
   Redirect 301 / http://www.example.com/
</VirtualHost>

<VirtualHost 1.2.3.4:80>   
   DocumentRoot /home/example/www/example.com/public_html/
   ServerName www.example.com

   <Directory /home/example/www/example.com/public_html/>
      AllowOverride all
   </Directory>

</VirtualHost>

<VirtualHost 1.2.3.4:80>
   DocumentRoot /home/example/www/%1.example.com/public_html/
   ServerName subs.example.com
   ServerAlias *.example.com

   <Directory /home/example/www/%1.example.com/public_html/>
      AllowOverride all
   </Directory>
</VirtualHost>
Jenny D
  • 27,780
  • 21
  • 75
  • 114
  • You say you've read lots of pages. Was http://httpd.apache.org/docs/2.2/vhosts/mass.html among them? Did you enable mod_vhost_alias? What do your logs say for connections that don't behave as you expact? – Jenny D Jul 15 '17 at 09:57

1 Answers1

0

If you want to create wildcard subdomains to others dir (in example: test.example.com -> /var/www/test.example.com , and ex.example.com -> /var/www/ex.example.com . You must Load mod_vhost_alias module:

LoadModule vhost_alias_module modules/mod_vhost_alias.so

and then you can create (example this) configuration file:

<VirtualHost *:80>
   ServerName test.com
   Redirect 301 / http://www.test.com/
</VirtualHost>

<VirtualHost *:80>   
   DocumentRoot /var/www/test.com/
   ServerName www.test.com
</VirtualHost>

<VirtualHost *:80>
   VirtualDocumentRoot /var/www/%0
   # You can make here:
   # VirtualDocumentRoot /var/www/%0/public_html
   ServerAlias *.test.com

</VirtualHost>

this is my structure of dirs in /var/www:

tree
.
├── haha.test.com
│   └── index.html
├── html
│   └── index.html
├── subs.test.com
│   └── index.html
├── test
│   └── index.html
└── test.com
    └── index.html

6 directories, 6 files

I have tested this on VirtualMachine Debian 9.0 and Apache/2.4.25 (Debian).

liske1
  • 114
  • 4