Forum Moderators: phranque
# Extract subdomain if present
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
# If html filetype and subdomain is present, store it into user-defined variable and skip next rule
RewriteRule \.*$ - [E=subd:%2,S=1]
# No subdomain or non-html filetype, so exit
RewriteRule .* - [L]
# Rewrite html files using stored subdomain
RewriteRule ^list_(.*)_(.*).(.*) /%{ENV:subd}[QSA,L]
RewriteRule ^(edit¦show)_(.*).(.*)/%{ENV:subd[QSA,L]
But it is not working..can somebody help me here? Note, my urls will be very generic..there can be many levels of subdirectories in the subdomain urls and also some URLs will have query parameters..
What URLs did you use to test it?
What were the results?
How did those results differ from your expectations?
We will need *a lot* of detail about your proposed URL structure in order to help. For example, explain the variations of the "list" "edit" and "show" subdirectories, and why those need to be handled separately.
Jim
I removed it. Problem is it just goes to the main directory & not the subdirectory. Please note, I have also entry of virtual host as follows.
<VirtualHost --ip address-->
DocumentRoot "D:\hshome\vendrang\apache2\Apache2\htdocs\sites\--main-dir--"
ServerName www.example.com
</VirtualHost>
and now the updated httpd.conf contains
# Extract subdomain if present
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
# If html filetype and subdomain is present, store it into user-defined variable and skip next rule
RewriteRule \.*$ - [E=subd:%2,S=1]
# No subdomain or non-html filetype, so exit
RewriteRule .* - [L]
# Rewrite html files using stored subdomain
RewriteRule (.*)_(.*).(.*) /%{ENV:subd}[QSA,L]
i expect if someone types [xyz.example.com...]
then I should content from
D:\hshome\vendrang\apache2\Apache2\htdocs\sites\--main-dir--\xyz\firstdir/test.jsp?id=8
and if someone types
[abc.example.com...]
then I should content from
D:\hshome\vendrang\apache2\Apache2\htdocs\sites\--main-dir--\abc\firstdir/test.jsp?id=8
Requirement given to me is to maintain the url in client's browser only as [abc.example.com...] & not the real content URL
currently i type [abc.example.com...] or [xyz.example.com...] it always shows content from main dir & not subdirectories under main directory
This can be overcome by mapping robert.example.com to example.com/user-robert instead of mapping it to example.com/robert. If you don't do this, then someone might easily break your site in Internet Explorer by signing up as user "w3c" and posting a faulty privacy policy in their space. Or worse, by signing up as username "www".
So, to prevent these problems, you simply tag each subdomain-subdirectory with "user" or some other relatively unique identifier.
Given that, you code can be simplified, and we can add insurance for the case I described:
httpd.conf:
# As long as request is not for "www.example.com" or "<anything>.www.example.com", etc.
RewriteCond %{HTTP_HOST} !www\.example\.com
# Extract subdomain (must start with lowercase letter, and be all-lowercase letters, numbers, hyphen, or underscore only, 3 to 12 characters total)
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z][a-z0-9_\-]{2,11})\.example\.com
# Do not rewrite requests for username.example.com/user_<username> (and see next rule below)
RewriteCond $1 !^user_
# Rewrite subdomain to subdirectory
RewriteRule ^/(.*)$ /user_%2/$1 [L]
#
# Forbid client attempts to discover or access username subdirectories directly
# (Prevent direct access at www.example.com/user_<username>)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /user_
RewriteRule ^/user_ - [F]
This code does not manipulate or change query strings; They will be passed through unchanged (The [QSA] flag is not needed.)
An alternative for this somewhat 'complex' method is to use Apache mod_user_dir [httpd.apache.org].
Jim
DocumentRoot "F:/Apache2/htdocs/public/example"
Options +FollowSymLinks
RewriteEngine on
# As long as request is not for "www.example.com" or "<anything>.www.example.com", etc.
RewriteCond %{HTTP_HOST}!www\.example\.com
# Extract subdomain (must start with lowercase letter, and be all-lowercase letters, numbers, hyphen, or underscore only, 3 to 12 characters total)
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z][a-z0-9_\-]{2,11})\.example\.com
# Do not rewrite requests for username.example.com/user_<username> (and see next rule below)
RewriteCond $1!^user_
# Rewrite subdomain to subdirectory
RewriteRule ^/(.*)$/user_%2/$1 [L]
#
# Forbid client attempts to discover or access username subdirectories directly
# (Prevent direct access at www.example.com/user_<username>)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /user_
RewriteRule ^/user_ - [F]
I have subdirectories like user_test, user_product inside the document root directory F:/Apache2/htdocs/public/example ..any suggestions? also will you please explain the meaning of this part ^/(.*)$/ in the rule ^/(.*)$/user_%2/$1 [L] that will make me understand the path scenario better.. Thanks again..
You will probably need to declare a 'wild-card' subdomain ServerAlias in order to get your subdomains to work.
Something like
ServerAlias *.example.com
For more information, see the documentation at [httpd.apache.org,...] including the 'core' Apache directives which include directives for declaring the domains that the server should handle. There is also a tutorial on setting up name-based virtual hosting which may clarify some points. In addition, our charter contains citations of various tutorials and documentation. Of interest would be the regular-expressions tutorial, which explains the regex patterns used for mod_rewrite patterns.
Jim
<VirtualHost --ip-address-->
DocumentRoot "F:\apache2\htdocs\example\"
ServerName www.example.com
ServerAlias *.example.com
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST}!www\.example\.com
# Extract subdomain (must start with lowercase letter, and be all-lowercase letters, numbers, hyphen, or underscore only, 3 to 12 characters total)
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z][a-z0-9_\-]{2,11})\.example\.com
# Do not rewrite requests for username.example.com/user_<username> (and see next rule below)
RewriteCond $1!^user_
# Rewrite subdomain to subdirectory
RewriteRule ^/(.*)$ /user_%2/$1 [L]
#
# Forbid client attempts to discover or access username subdirectories directly
# (Prevent direct access at www.example.com/user_<username>)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /user_
RewriteRule ^/user_ - [F]
</VirtualHost>