Forum Moderators: phranque
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
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.
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]
Jim
RewriteCond %{REQUEST_URI} !^/use[b]rs/[/b]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /users/%1/$1 [L]
Jim
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