Forum Moderators: phranque
I am running apache 1.3 on my Mac localhost. I have a series of virtual hosts set up. One of them I want to be able to create subdomains for. The idea is that I can use test.example.com and it will map to a directory called test.example.com within the web root.
It is exactly what mediatemple allow with their hosting in that you just create a dir named something.example.com and it creates a sub domain with that name.
Here is the .htaccess file I have but it doesn't seem to be doing anything.
<Files .*> #this is only for allowing non-type files, like domain.com/blog
Order Deny,Allow
Allow From All
</Files>
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.com [NC]
RewriteRule ^(.*) http://www.example.com/%1 [R=301,L]
I have also read about needing to set wildcard DNS on but I have not found out how to do this and reading other tutorials I am not sure that is necessary.
Hope someone can help :D
James
[edited by: jdMorgan at 1:25 pm (utc) on May 24, 2005]
[edit reason] Example.com. [/edit]
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule ^/(.*) /%1[b]/$1[/b] [L]
There are three possible solutions if you must run this code in .htaccess. One is to explicitly test each each possible subdomain-subdirectory name, and inhibit the rewrite if it has already been added to the URL-path. Another is to use a 'unique identifier' on the subdirectory names, such as a prefix of "sd_" or "sub-", and check for that before rewriting. And finally, if you are running on a server whose OS support POSIX 1003.2 regular expressions, you can implement a 'compare' function to test if the the top-most subdirectory name matches the requested subdomain name, and inhibit further rewriting if so. This method is complex, only works on a few servers, and is described here [webmasterworld.com].
Please be very specific in this forum when you report test results. Saying "It doesn't do anything" or "It doesn't work" provides almost no useful information to those who might otherwise help you. All of these are important and will help you get better answers:
See the references in our forum charter [webmasterworld.com] for more info on mod_rewrite and regular expressions.
Jim