tags:

views:

36

answers:

2

How would one go about implementing a vanity URL for each user in PHP? I'm implementing my web-app's login system as a tweaked version of the Drax LLP Login system.

So, each user should be able to modify his profile which will finally appear on his vanity URL .. like xyz.com/user.

Any tips / ideas? Thanks..

A: 

It's relatively simple: you have a mod_rewrite rule to map requests to www.domain.com/username to something like www.domain.com/users/username.

However, you then need to be aware what to prohibit from being a username, as I presume you'll have other top-level pages such as www.domain.com/about or www.domain.com/terms. Therefore, you don't want people registering about and terms as usernames.

Martin Bean
OK I'll take a look into that :) I'm new to PHP and still delving through the manual.. Thanks. Also, thanks for the tip on the exception of usernames :)
Hardik Ruparel
One method you could use is when a user is registering a user name, to check with `cURL` or similar whether that URL already exists (`200 OK`). If so, deny them the ability to register it.
Martin Bean
The cURL trick is clever, and certainly efficient if the frontend doesn't have easy access to the data store (e.g. preparing to send a request to a web service).
Jeff Standen
Thanks to all you guys :) I got it to work :)
Hardik Ruparel
A: 

Here's an example of the files involved:

.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>

index.php

<?php
function getWebPath() {
    $location = "";

    // Read the relative URL into an array
    if(isset($_SERVER['HTTP_X_REWRITE_URL'])) { // IIS Rewrite
        $location = $_SERVER['HTTP_X_REWRITE_URL'];
    } elseif(isset($_SERVER['REQUEST_URI'])) { // Apache
        $location = $_SERVER['REQUEST_URI'];
    } elseif(isset($_SERVER['REDIRECT_URL'])) { // Apache mod_rewrite (breaks on CGI)
        $location = $_SERVER['REDIRECT_URL'];
    } elseif(isset($_SERVER['ORIG_PATH_INFO'])) { // IIS + CGI
        $location = $_SERVER['ORIG_PATH_INFO'];
    }

    return $location;
}

$location = getWebPath();

print_r($location);

The function above should become a method in your front controller class somewhere. You just need to subtract your base path from $location so you're left with the virtual path. Then you can do whatever you want -- e.g. simply split on '/' and then work your way through the path, passing each remainder to the next controller depending on the URI. You could also write more complicated routing rules using regular expressions (something Django and quite a few other frameworks do).

For example: /profiles/hardik988

Would be passed to the controller in charge of 'profiles', which then decides what to do with the remainder of the path. It would likely look up the username and display the appropriate template.

Jeff Standen
Thanks guys. I got it to work out :)
Hardik Ruparel