Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite problem - sub domain to sub folder of a subfolder

mypage.domain.com -> domain.com/users/mypage

         

xgenesisx

8:03 pm on Oct 20, 2007 (gmt 0)

10+ Year Member



Hi,

I am going to go insane - I am trying to rewrite the subdomain to a subfolder of the "users" folder.

mypage.domain.com -> domain.com/users/mypage

Here is the .htaccess I am trying to use:

RewriteEngine on
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.domain\.com(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3!^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) /users/%1/$1 [L]

This is not working.

When I change the last line to:
RewriteRule ^(.*) [domain.com...] [L]

It is fine, but browser address line changes to this long domain I would prefer to hide.

Also, when I change the last line to this:
RewriteRule ^(.*) /%1/$1 [L]
It is working, I am able to access the direct subfolders of the root folder.

Is there anybody out there who can help me? This forum was already really useful - but I just cannot make this simple modification myself and I do not have a clue why...

Thanks for the help in advance,
Norbert

jdMorgan

8:29 pm on Oct 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you sure, or have you verified that your server's OS supports POSIX 1003.2 regular expressions or later? Some do, but many don't. Only POSIX 1003.2 and later support the 'atomic back-reference (the "\1" in the code) that is required for the 'compare' function in the code to work as intended.

Your post concludes that you tried two modified rules that work, so it's not clear what your current problem is. However, you should look carefully at the comment for the third RewriteCond: This code is designed to compare the first URL-path element to the requested subdomain. In your case, I believe you'll want to compare the second URL-path element to the requested subdomain, and the first URL-path element should always be "users". So, it's likely you'll want to account for those differences in the code.

If the compare function is not correct, then the code will recurse, rewriting joe.example.com/xyz to /users/joe/xyz and then rewriting that to /users/joe/users/joe/xyz, and on and on, until either the server or browser gives up.

Just a comment on comments. When modifying code, be sure to modify the comments as well to indicate your intent; Otherwise, you may have problems in the future, after you've forgotten all the details...

This code, when posted on a public forum, should always make clear the requirement for POSIX 1003.2 or later.

Jim

xgenesisx

3:50 pm on Oct 21, 2007 (gmt 0)

10+ Year Member



Hi,

Thanks, I have a solution - there was a problem with the recursion. I solved it by checking the URI for "users".

Here is it:

RewriteEngine on
# Rewrite <subdomain>.domain.com/<path> to domain.com/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.domain\.com(:80)?<>/([^/]*) [NC]
# Rewrite only when the path does not contain users already
RewriteCond %{REQUEST_URI}!^.*users.* [NC]
# Rewrite to users/subdomain/path
RewriteRule ^(.*) /users/%1/$1 [L]

Thanks again,
Norbert

jdMorgan

4:09 pm on Oct 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, you don't need the last half of the third RewriteCond, and you can clean up your REQUEST_URI pattern to make it more efficient:

RewriteEngine on
# Rewrite <subdomain>.domain.com/<path> to domain.com/users/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Rewrite only when the path does not already contain users
RewriteCond %{REQUEST_URI} !^/users/
# Extract requested subdomain (%1)
RewriteCond %{HTTP_HOST}^([^.]+)\.example\.com(:80)?$
# Rewrite to users/subdomain/path
RewriteRule (.*) /users/%1/$1 [L]

Jim