1

Basically I want *.domain.com to pull up domain.com/*/ (not redirect).

I went into the subdomains section of cPanel and set a wildcard for this domain. It appears to be resolving correctly, ie *.domain.com is bringing up domain.com.

I've now made a htaccess file in the public_html directory containing:

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule (.*) %2/$1 [L]

The error I'm getting is '500 Internal Server Error', any ideas?

Joe
  • 1,535
  • 1
  • 10
  • 15
zuk1
  • 141
  • 1
  • 12

2 Answers2

2

Refer to this StackOverflow question: "Create Subdomains on the Fly With .htaccess"

See answers on the question, specifically this link which has examples which should work in your situation.

There are a couple of issues that the Webmasterworld post addresses, including subdomain recursion since the .htaccess also impacts your subdirectories in your main www root.

The final code is like so:

RewriteBase /

#### URL Rewrite Handler for Subdomains (by Randall Krause) ####

RewriteCond %{ENV:REDIRECT_SUBDOMAIN} ="" 
RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.mydomain\.org\.?(:80)?$ [NC] 
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1 -d 
RewriteRule ^(.*) subdomains/%1/$1 [E=SUBDOMAIN:%1,L] 
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L] 

If you want to see what your above code errors out on (what is creating the 500 error), check your error_log. My guess is that it is causing recursion.

Dave Drager
  • 8,315
  • 28
  • 45
0

I would use mod_vhost_alias. For example:

VirtualDocumentRoot /usr/local/apache/vhosts/%-3+

Would map requests as follows:

  http://dom1.example.com/      =>  /usr/local/apache/vhosts/dom1/
  http://foo.dom2.example.com/  =>  /usr/local/apache/vhosts/foo.dom2/
  etc...

You can of course tailor the way the mapping works. See the documentation for all the details and more examples.

Joe
  • 1,535
  • 1
  • 10
  • 15