0

I am working on a social networking site. Something that I would like to do is have the user's username come after my websites name... like example.com/myusername. I know I could do something using $_GET variables where it would look like example.com/?u=myusername, but we think that having just the username after after the domain would look cleaner and be easier for users to navigate.

Thanks in advance,

JS

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Tim Powell
  • 127
  • 1
  • 11
  • What web server? There are many examples available. – Michael Berkowski Dec 01 '12 at 14:47
  • I suggest you use `example.com/user/username` - that way there are less restrictions on usernames, because users may want to have names like `404`, `index.html`, etc. – scriptin Dec 01 '12 at 14:50
  • right now, im using a single IIS server... today or tomorrow i'll be switching to a distibuted site using apache httpd with php5 on ubuntu server 12.04 – Tim Powell Dec 01 '12 at 15:02

2 Answers2

2

You can setup a rewrite rule to direct all non-existent requests to your index.php (or really whatever script you want to use). Then just inspect the $_SERVER['REQUEST_URI'] and pull out the username.

Here's a basic set of rewrite rules (for apache):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

And in your case:

$requestParts = explode('/', $_SERVER['REQUEST_URI'];
$username = $requestParts[0];

That would allow you to also use: example.com/username/posts/newest, and pull out the various 'parts'.

Tim Lytle
  • 17,549
  • 10
  • 60
  • 91
1

If you are using apache then look for mod_rewrite.
If you are using IIS then read here: mod_rewrite equivalent for IIS 7.0.

Use either to parse the calling URL and build vars for your page.

Community
  • 1
  • 1
ethrbunny
  • 10,379
  • 9
  • 69
  • 131