Forum Moderators: phranque
Many thanks for any tips/pointers!
Fletch.
More detail is needed. Does 'example' contain hyphens or other punctuation.
Ah, you're talking 'rewrite' not redirect, so what is the internal server path that such a request will resolve to, and what URL request will match that?
Protocol (HTTP/HTTPS), subdomain, domain, FQDN/non-FQDN indicator (trailing period on domain), port number, URL-path, query, and fragment identifiers may all vary, and if so, these variations must be accounted for. While in some cases, it's easy to say "all," this is usually not the case, and further, this can complicate efforts to prevent 'infinite' redirection loops.
For example, a requested URL could well be "http://www.subdomain.example.com.:80/url-path1.html?sid=1234#named-anchor", or it might be "https://example.com/user-1234/"
Redirection and rewriting generally require the use of regular-expressions patterns to "decide" whether and what to redirect/rewrite. The allowed variations in the requested URL must be taken into account when writing these patterns. So a list of URLs demonstrating the nature and range of variations is most useful.
Otherwise, you'll have requests which don't get redirected/rewritten as desired and/or requests which do get rewritten/redirected when this is not desired.
The basic plan is to check the request variable HTTP_HOST and if non-blank, extract the hostname and inject it into the URL-path. However, there are likely some exceptions such as "www", where it's unlikely that you want to treat this subdomain as a "username."
Also, to prevent future performance and functional problems, it's a good idea to rewrite all "user-subdomain" requests to a separate subdirectory, so that collisions between usernames and directories required for site infrastructure cannot occur. For example, rewrite <username>.example.com/foo.html to /users/<username>/foo.html instead of rewriting it to /<username>/foo.html
A basic http-only example using mod_rewrite in a server config file, outside of any <Directory> containers, would be:
# Externally redirect to remove leading "www" from subdomain requests and force canonical
# domain (add "www" if missing, remove FQDN indicator and port number if present)
RewriteCond www.%{HTTP_HOST} ^(www)\.(www\.)?example\.com\.(:[0-9])?$ [OR]
RewriteCond www.%{HTTP_HOST} ^(www)\.(www\.)?example\.com:[0-9]$ [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule ^/(.*)$ http://%1.example.com/$1 [R=301,L]
#
# Internally rewrite all "username" subdomain requests except "www" to
# "users/<username>" subdirectory
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^/(.*)$ /users/%1/$1 [L]
If this code is to be deployed in a .htaccess file, then loop-prevention will be required. Add a rewritecond to the second rule:
RewriteCond %{REQUEST_URI} !^/users/
[added] Also, if this code is used in .htaccess or in a server config file within a <Directory> container, the leading slash must be removed from both rules' patterns. [/added]
Jim