Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite problem

getting subdomains to map to directories

         

konkrete

10:01 am on May 24, 2005 (gmt 0)

10+ Year Member



Hey guys,
I would just like to say now that I am not really familiar with the syntax for mod_rewrite I have just been trying examples already posted on these forums and got nowhere fast. Anyway, here is my problem.

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]

jdMorgan

1:58 pm on May 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A problem with that code is that it will drop the requested file -- all requests will end up going to the index page in the subdirectory, no matter which page is requested. Next, the literal periods in regex patterns need to be escaped with a "\" as shown. Lastly, you probably will want to use an internal rewrite so as to *not* change the user's browser address bar from "sub.example.com/" to "sub.example.com/sub/"

RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule ^/(.*) /%1[b]/$1[/b] [L]

The modified code is intended for use in httpd.conf. You will find it impossible to get it to work in .htaccess, because it allows for the use of arbitrary subdomains. The problem is that in .htaccess, you will get an 'infinite' loop. This is because the code does not check that the rewrite to the subdirectory has already occurred, and in .htaccess, code behaves as if it were recursive. After a rewrite in .htaccess, the server will transfer control back to httpd.conf, and then back through any .htaccess files in the new URL-path. It does this so that any further rewrites or access controls on the new URL-path can be invoked. So the result with this code in .htaccess is that the new URL (with the subdomain-subdirectory added) will be processed through the same rewrite rule again, adding another level of subdirectory, and then again, ad infinitum, until the server reaches its maximum internal redirect limit.

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:

  • How did you test? -- What did you type?
  • What error messages dod you get?
  • What showed up in your server error log?
  • How did the test results differ from your expectations?

    See the references in our forum charter [webmasterworld.com] for more info on mod_rewrite and regular expressions.

    Jim

  •