Forum Moderators: phranque
You will need to 'publish' your links on your pages using the subdomain naming convention, and then use mod_rewrite to rewrite those links, when requested from your server, back into the form needed to call your script. mod_rewrite works on URLs as they are received by your server during the URL-to-filename translation phase, and has no capability to change the content of your pages (in the content-handler phase). As a result, mod_rewrite works 'backwards' from the perspective of your question, but it *is* used to implement subdomain-handling with a scripted application.
Try a site search for subdomain rewriterule rewritecond HTTP_HOST script and variations -- there are several previous threads on this subject, and some background information in the forum charter [webmasterworld.com] and tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim
It seems he wants to do exactly what i want. I just replaced mydomain with my actual site name, and replaced index.php with profile.php. I thought it would work, but when i go to myname.mydomain.com, i get a "server cannot be found" error, and when i go to www.mydomain.com, i get a "username not registered" error..
:(
You will need to do three things to make this code work:
If you have admin access to httpd.conf, you can skip the second two steps, and set up virtual hosts for your subdomains.
> and when i go to www.mydomain.com, i get a "username not registered" error.
This doesn't sound like an Apache error at all. It may be one of your scripts, or one of your hosting companies scripts -- I can't tell.
Jim
That should work, but on the www.mysite.net, i get the "username not found" error, and when i enter an existing username like username.mysite.net i get a "cannot find server".. Very strange. The code im using is below
RewriteEngine on
# If no-www domain requested, externally redirect to www domain
RewriteCond %{HTTP_HOST} ^mysite\.net
RewriteRule (.*) [mysite.net...] [R=301,L]
#
# If www+subdomain domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mysite\.net
RewriteRule (.*) [%1.mysite.net...] [R=301,L]
#
# If subdomain+www domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.mysite\.net
RewriteRule (.*) [%1.mysite.net...] [R=301,L]
#
# If www domain requested, rewrite html page requests to profile.php with query string
RewriteCond %{REQUEST_URI}!^/profile\.php
RewriteCond %{HTTP_HOST} ^www\.mysite.net
RewriteRule ^([^.]+)\.html /profile.php?page=$1 [L]
#
# If subdomain requested, rewrite subdomain and html page requests to profile.php with query string
RewriteCond %{REQUEST_URI}!^/profile\.php
RewriteCond %{HTTP_HOST}!^www\.mysite\.net
RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.net
RewriteRule ^([^.]+)\.html /profile.php?user=%1&page=$1 [L]
#
# If www domain requested, rewrite home page requests to profile.php with query string page = "home"
RewriteCond %{REQUEST_URI}!^/profile\.php
RewriteCond %{HTTP_HOST} ^www\.mysite.net
RewriteRule ^$ /profile.php?page=home [L]
#
# If subdomain requested, rewrite home page requests to profile.php with query string user=subdomain & page="home"
RewriteCond %{REQUEST_URI}!^/profile\.php
RewriteCond %{HTTP_HOST}!^www\.mysite\.net
RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.net
RewriteRule ^$ /profile.php?user=%1&page=home [L]
# If non-www domain requested, externally redirect to www domain
RewriteCond %{HTTP_HOST} ^mysite\.net
RewriteRule (.*) http://www.mysite.net/$1 [R=301,L]
#
# If www+subdomain or subdomain+www domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mysite\.net [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.mysite\.net
RewriteRule (.*) http://%1.mysite.net/$1 [R=301,L]
#
# if home page requested, rewrite to 'home.html' and pass through to following rules
RewriteRule ^$ /home.html
#
# If www domain requested, rewrite html page requests to profile.php with query string
RewriteCond %{HTTP_HOST} ^www\.mysite.net
RewriteRule ^([^.]+)\.html$ /profile.php?page=$1 [L]
#
# If subdomain requested, rewrite subdomain and html page requests to profile.php with query string
RewriteCond %{HTTP_HOST} !^www\.mysite\.net
RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.net
RewriteRule ^([^.]+)\.html$ /profile.php?user=%1&page=$1 [L]
Once you have determined where the problem is, then look at your raw server error log after requesting a URL that will activate rule and cause the/an error, and see what the server's specific complaint is. This could be caused by any number of things -- including a few that I didn't list in my first post. but your code is basically OK.
Jim
Basically anything besides www as a sub domain causes a server not found error. Is this something i have to take up with my host?
For this one, contact your host:
Jim
No, not necessarily. ".*" is the wildest of wildcards, and means "any number (including zero) of any characters."
In the example code here, we use "[^.]+" instead, meaning "one or more characters, not equal to a period (dot, full stop, etc.). It is a far more efficient pattern, because it allows the regex parser to immediately know where to stop matching characters into the %1 variable: We told it to stop when it finds the period.
Otherwise, using ".*", the parser will throw the entire requested hostname into %1, and then realize it can't match the rest of the pattern. So, it will back off one character from the end and try again, repeating this backoff-and-try-again until it finally gets a match with only the subdomain left in %1. Therefore, the required number of parser passes is directly proportional to the length of the string folling the subdomain name in the requested hostname. Using the "[^.]+" pattern, the parser only needs one pass.
Avoid using ".*" whenever possible. It is possibly the easiest pattern to learn/use, but also the least efficent and often the most troublesome. The functional descriptors for the ".*" pattern are "ambiguous," "greedy," and "promiscuous." Although these terms are used to describe its technical pattern-matching behaviour, they still carry the stigma normally associated with those words.
Jim