Forum Moderators: phranque
My host currently offers unlimited subdomains with their accounts, but it runs through some funky frames system which totally kills any possible SEO... I've seen scripts that work with ssi and/or meta refresh tags... both bad options.
How are other people implementing subdomains on their sites? (Hopefully paynt will see this thread... ;) ) I can have domain wildcards turned on for my account, if necessary.
See sticky mail.
Seems like mod_rewrite should be able to do it, but I haven't found a ready-written solution yet (found two that seem to be "close but not quite"), and I certainly don't know how to write mod_rewrite parameters/conditions/rules/whatever-you-call-them myself... :)
But it's not easy to understand or write for if you're a beginner like I am, so I'm hoping some mod_rewrite genius will come along and give me the answer. ;)
NameVirtualHost 111.222.111.222
<VirtualHost 111.222.111.222>
DocumentRoot /path/to/sub1.domain.com/
ServerName sub1.domain.com
CustomLog /path/to/sub1.domain.com.log
</VirtualHost>
<VirtualHost 111.222.111.222>
DocumentRoot /path/to/sub2.domain.com/
ServerName sub2.domain.com
CustomLog /path/to/sub2.domain.com.log
</VirtualHost>
and so on.
The above example uses separate logs, but they all could go into one central log if you wanted.
If your web hosting company isn't run by nice people you could try this mod_rewrite solution
A mod_rewrite approach:
1) Put this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.com$
RewriteRule ^(.+)$ %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+)\.domain\.com(.*) $1/$2 [L]
2) You need to have your domain wildcarded, So www.domanin.com and xyz.domain.com will go to the same folder.
3) You need to put an overriding .htaccess in your subdomain folders.
So if you are going to use products.domain.com you need to have /path/to/domain/products use it's own .htaccess folder with an overwriting mod_rewrite command. It could be as simple as "RewriteEngine off" at the top of an otherwise blank page.
That should be it.
So now blahh.domain.com/123.html will be internally directed to /path/to/domain/blahh(the sub-folder)/123.html
The second approach is more resource intensive, so you should try the first approach if your hosting company makes it possible.
I know I can get wildcards turned on. Whether or not I can talk them into editing the httpd.conf file for me is yet to be seen... but if I know specifically what to ask for, my odds are probably better. Thanks!
Anyway, mivox:
I think when you have one Virtual Host setup in apache, it becomes the default. When you have multiple VH sections, apache uses the first as default. Then, all requests to unknown subdomains (not handled explicitely in httpd.conf) will be sent to it.
The problem is to setup the DNS records in the first place. You need to be able to add CNAME records, or at least you get them to do it for you.
littleman:
I found that gem in the apache manual too, and after experimenting with it for a few minutes, i've found a few problems...
1) Turning RewriteEngine Off in the subdomain folders gives me a 403 Forbidden error.
2) Folders in the main domain don't work anymore! (403 also)
3) I get a "Bad Rrequest" for accessing the subdomains with everything you mentioned setup right.
Another issue that may need considering: banning people from accessing [domain.com...] directly. I would show you my code for this, but I'll wait until everything is working perfectly ;)