Forum Moderators: phranque
subdomain1.domain.com should render as domain.com/subdomain1 in the address bar
# the rewrite rule below will be applied only if this condition passes
# this condition tests if the user came in through one of the subdomains
# it will also remember the subdomain value (using parentheses)
RewriteCond %{HTTP_HOST} ^(subdomain1|subdomain2|subdomain3)\.example\.com$
# the subdomain value we captured from the condition can be used in the rewrite substitution as %1
# and the path value we capture from the rewrite pattern below can be used as $1
# by default, this would issue a temporary (302) redirect
# the [R] flag lets us change this to a permanent (301) redirect
RewriteRule (.*) http://example.com/%1/$1 [R=permanent] RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
I have a set of subdomains that need to render in the browser as a subfolder structure, even though the content of each does not reside in these subfolders.
subdomain.example.com/morestuff www.example.com/subdomain/morestuff RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.com
RewriteRule (.*) http://www.example.com/%1/$1 [L,R=301]
subdomain1.domain.com should render as domain.com/subdomain1 in the address bar
address bar says
subdomain.example.com/morestuff
while their browser serves up material from
www.example.com/subdomain/morestuff
Instead:
address bar says
example.com/sudomain1
while their browser serves up material from
subdomain1.example.com/
their browser serves up material from
subdomain1.example.com
# main domain htaccess
# any request for example.com/subdomain/* should be fulfilled
# by another server located at subdomain.example.com
RewriteRule ^(subdomain1|subdomain2|subdomain3)(?:/(.*))?$ http://$1.example.com/$2 [P] # subdomain htaccess
# the redirect below will be applied only if this condition passes
# this condition tests if the current request is *not* a proxy request from the main domain
RewriteCond %{HTTP:X-Forwarded-Host} !^example.com$
# this additional condition may be redundant
# it's useful if the main domain and subdomains share the same htaccess file
# that doesn't sound like it's the case here
# yet we still need this condition so that we can capture the subdomain value
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.com$
# the subdomain value we captured from the condition can be used in the rewrite substitution as %1
# and the path value we capture from the rewrite pattern below can be used as $1
# by default, this would issue a temporary (302) redirect
# the [R] flag lets us change this to a permanent (301) redirect
RewriteRule (.*) http://example.com/%1/$1 [R=permanent]
I will eventually want to implement this on Apache
[edited by: phranque at 12:02 am (utc) on Apr 30, 2013]
[edit reason] unlinked example.com url [/edit]
I have seen reference to leading slashes, but nothing too clear.
The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.