Forum Moderators: phranque
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
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
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
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]