5.21. Rewriting a Hostname to a Directory
Problem
You want requests for http://bogus.example.com/ to be turned into requests for http://example.com/bogus/.
Solution
RewriteCond "%{HTTP_HOST}" "^([^.]+)\.example\.com" [NC]
RewriteRule "(.*)" "http://example.com/%1$1" [R]"To do this transparently, without a redirect:
RewriteCond "%{HTTP_HOST}" "^([^.]+)\.example\.com$" [NC]
RewriteRule "(.*)" "/%1$1" [PT]Discussion
This technique is occasionally needed when wildcard hostnames
are being supported. It gives the illusion that URLs are pointing to
separate hosts rather than subdirectories on only one system. Of
course, using the redirection ([R])
flag will void the illusion because the replacement URL will be
visible to the end user. If you want it to be completely transparent
to the user, you can use the second option to get the equivalent
result with a rewrite internal to the server that the client never
sees.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access