Forum Moderators: phranque

Message Too Old, No Replies

subdomains with htaccess

         

magnum

4:38 pm on Jul 18, 2005 (gmt 0)

10+ Year Member



Hi,

I am running a free subdomain hosting service, using wildcard, dedicated IP and redirection using htaccess file like this:

RewriteCond %{HTTP_HOST} ^sub1.domain.com$
RewriteCond %{REQUEST_URI}!sub1/
RewriteRule ^(.*)$ sub1/%{REQUEST_URI}

RewriteCond %{HTTP_HOST} ^sub2.domain.com$
RewriteCond %{REQUEST_URI}!sub2/
RewriteRule ^(.*)$ sub2/%{REQUEST_URI}
....................

This works fine, but there are a lot of records like this, and I need something like this:

RewriteCond %{HTTP_HOST} ^anysub.domain.com$
RewriteCond %{REQUEST_URI}!anysub/
RewriteRule ^(.*)$ anysub/%{REQUEST_URI}

is it possible?

Regards

jdMorgan

7:41 pm on Jul 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes and no... It depends on whether your operating system supports POSIX 1003.2 regular expressions, or if you have access to httpd.conf.

If you have POSIX 1003.2 support, see message #6 in this thread on rewriting arbitrary subdomains to subdirectories [webmasterworld.com].

If you have httpd.conf access, then you can put the code there; The RewriteCond used for preventing a rewrite loop will not usually be needed in httpd.conf, which makes the second RewriteCond unnecessary and simplifies the code so that one rule can be used for all subdomains.

If neither of these is true, then you'll have to handle the subdomains on a case-by-case basis.

Even in .htaccess, you could compress the logic a bit using something like this, though:


RewriteCond %{HTTP_HOST} ^(sub1¦sub2¦sub3)\.domain\.com
RewriteCond %{REQUEST_URI} !^/ss_
RewriteRule (.*) /ss_%1/$1 [L]

This requires that all of your subdomain-subdirectory names start with the unique name "ss_".

Do not end-anchor domain names compared to the HTTP_HOST variable. HTTP_HOST can contain a port number appended to the hostname, and that will break your rule if the pattern is end-anchored. Example: www.example.com:80 would case your RewriteCond above to fail.

Be sure to change the broken pipe "¦" characters above to solid pipe characters before use. Posting on this board modifies them.

Jim