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?


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 %{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] 

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.


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/

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.