Question

I am out of my depth here, currently reading the tutorials and using python to learn regex.

I have a website where a php file http://www.example.com/showme.php?user=JOHN will load the visitor page of JOHN. However I want to let John have his own vanity URL like john.example.com and rewrite it to http://www.example.com/showme.php?user=JOHN .

I know it can be done and after fiddling with it it seems lighttpd mod_rewrite is the way to go. Now I am stumped as I am trying to come up with regex to match!

rewrite ("^![www]\.example\.com" => "www\.example\.com\?user=###");

I am playing with python re module to test out several ways of getting the john from john.example.com and recognize when the first segment of url is not www and then redirect. Above was my trial. Am I even in the right continent!

Any help will be appreciated in

  1. recognizing when first part of url before the first . is not www and is something else - so that example.com won't stump it.
  2. getting the first part of the url before first . and tag it to user=###

Thanks a bunch

Was it helpful?

Solution

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"
        )
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top