Forum Moderators: phranque

Message Too Old, No Replies

Mod_rewrite: subdirectories -> subdomains

         

Varion

11:03 am on Apr 14, 2005 (gmt 0)

10+ Year Member



Hi, I'd like to convert subdirectories from a directory /users/ to subdomains. For example, I have [domain.com...] and I'd like to get shown [user1.domain.com....] I spent a lot of hours to resolve this problem, but I found only how to convert directories from root dir to subdomains (http://www.domain.com/users/ -> [users.domain.com)....] Below is this code. I hope somebody will know how to modify it. TIA.


RewriteEngine On
RewriteCond %{HTTP_HOST}!^$
RewriteCond %{HTTP_HOST}!^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
RewriteCond %2<->%3!^(.*)<->\1$ [NC]
RewriteRule ^(.+) /%2/$1 [L]

jdMorgan

1:38 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Varion,

Welcome to WebmasterWorld!

If you want to access your files by subdomains, then link to them as subdomains on your pages. Use the code you found to receive those subdomain request URLs, and convert them to subdirectory file-paths in order to retrieve the correct content.

I highlighted the critical words above: The URL shows a subdomain, but the files are stored in the filesystem under a subdirectory. Mod_rewrite executes after an HTTP request is received from the client, but before any content-handlers are activated or any scripts are invoked. In this case, mod_rewrite receives the requested URL and converts it into a filesystem path. Since subdomains don't exist in a filesystem, mod_rewrite can't rewrite subdirectories to subdomains.

So, the usual approach is this:

  • Refer to files in subdomain URLs by linking to subdomains on your pages. Users and robots will follow those links.
  • Use mod_rewrite to rewrite those subdomains, when requested by clients, to the proper location in the server filesystem.
  • If a direct request for a subdirectory is received from a client, redirect to the subdomain.

    A simple demonstration for use in the Web-root .htaccess would be:


    # Rewrite foo subdomain requests to foo subdirectory
    RewriteCond %{HTTP_HOST} ^foo\.example\.com
    RewriteCond %{REQUEST_URI} !^/foo/
    RewriteRule (.*) /foo/$1 [L]
    #
    # Prevent direct client access to foo subdirectory
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo [NC]
    RewriteRule ^foo(.+) http://foo.example.com$1 [R=301,L]

    Jim
  • Varion

    7:34 pm on Apr 14, 2005 (gmt 0)

    10+ Year Member



    Thanks for your reply. Unfortunately I can't use your code. I'm newbie and I don't know how to replace this foo :(

    jdMorgan

    8:20 pm on Apr 14, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Take a look a the documentation cited in our forum charter [webmasterworld.com], and do a few searches for similar tutorials on the Web. That's a very good way to get started. You have a working example above to use as a research sample. As noted in our charter, we're set up to answer specific questions here, and can't realistically write everybody's code for them.

    Jim