Use lighttpd's mod-rewrite module. Add this to your lighttpd.conf
file:
$HTTP["host"] != "www.example.com" {
$HTTP["host"] =~ "^([^.]+)\.example\.com$" {
url.rewrite-once = (
"^/?$" => "/showme.php?user=%1"
)
}
}
For an href
value like /dir/page.php
the domain part of the link gets automatically added from the current request as shown in the browser's address bar. So, if you had used www.example.com
; the link would point to htp://www.example.com/dir/page.php
and likewise for john.example.com
.
For all your links to point at www.example.com
, you need to be accessing the page using www
. This would be possible only if you do an external redirect from the vanity URL to the actual one i.e. users can still use the shortened URL but they would get redirected to the actual one.
$HTTP["host"] != "www.example.com" {
$HTTP["host"] =~ "^([^.]+)\.example\.com$" {
url.redirect = (
"^/?$" => "http://www.example.com/showme.php?user=%1"
)
}
}