Forum Moderators: phranque

Message Too Old, No Replies

Virtual subdomains using mod rewrite, just 1 rule

         

Rickvanhaas

5:37 pm on Jan 6, 2006 (gmt 0)

10+ Year Member



Hi all,

Currently, I am redirecting my visitors from:
user.domain.com
to:
www.domain.com/users/user

Using this rule:

RewriteCond %{HTTP_HOST} ^user.domain.com$
RewriteCond %{REQUEST_URI}!^/user/.*
RewriteRule ^(.*) /users/user/$1 [L]

This works great, but as the amount of users adds up, the number of lines does, too.

Is there a way to direct all possible usersnames (in subdomain-form) to the designated url, using just one command (i.e., treating the 'user' as a variable?)

Thanks a lot
Rick

jd01

8:56 pm on Jan 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure, you should be able to do what you are asking.

RewriteCond %{HTTP_HOST} ^user.domain.com$
RewriteCond %{REQUEST_URI}!^/user/.*
RewriteRule ^(.*) /users/user/$1 [L]

I would probably use something like this:

RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com
RewriteCond %{REQUEST_URI} !^/user/
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^(.*) /users/%2/$1 [L]

([^.]+) = 1 or more characters that are not a .(dot)

Hope this helps.

Justin

BTW what you have now works, but it is good practice to escape meta characters for an exact match.

jdMorgan

3:28 am on Jan 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I concur, with one small tweak:

Since the the RewriteCond that extracts the username from the subdomain requires the the subdomain be present, there is no need to make the subdomain optional in the RewriteCond that prevents 'www' from being treated as a 'user' subdomain. Therefore, we avoid wasting time creating a second (and unneeded) back-reference.


RewriteCond %{REQUEST_URI} !^/user/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /users/%1/$1 [L]

In addition, I corrected the formatting problem introduced by this forum -- spaces are required between the "}" and "!" characters in RewriteCond, but this forum deletes them.

Jim

Rickvanhaas

11:13 am on Jan 7, 2006 (gmt 0)

10+ Year Member



Works great, except for one thing: if the user doesn't exist, the page seems to come in some kind of loop...anything I can do about that?

Cheers

jdMorgan

3:44 pm on Jan 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I spotted a typo that may be responsible. I you haven't already done so, fix this first line:

RewriteCond %{REQUEST_URI} !^/use[b]rs/[/b]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /users/%1/$1 [L]

If it still loops after fixing that, please post the relevant lines from your server error log.

Jim

Rickvanhaas

4:18 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



I already found that one, so I left it out. The error log doesn't say anything (the page just keeps on loading, so no real error). The bug may be in one of my own scripts. If not, I'll post my solution here.

jdMorgan

4:31 pm on Jan 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take a look at how your 404s are handled, how the custom error documents (if any) are defined, and where they are defined to be located.

It's important to not let 'support' files, such as ErrorDocument files, robots.txt, /w3c/p3p.xml files, and any content-rating files you might have, get rewritten unless they also exist in the 'user' space.

If your user does not exist, but the ErrorDocuments are also rewritten to his/her filespace, then you'd get an infinite loop as the server tried to handle the 'user does not exist' error, it would get another error, because the custom error document would not exist either.

The addition of one (or a few) RewriteConds to exclude these files from being rewritten might be all that's needed.

Jim

Rickvanhaas

4:58 pm on Jan 7, 2006 (gmt 0)

10+ Year Member



There was indeed an error in the script. The .htaccess as described above works perfectly.