tags:

views:

103

answers:

4

Hi, i want something like this:

http://www.someniceandreliableurl.com/username

and catch the username.

I want to make something like twitters/facebook/etc quick urls...

twitter.com/username

How can i make something like this with php? =) thank you in advance.

+6  A: 

You can use a RewriteRule in apache to transform a site.com/user to be site.com/loadpage.php?user=user

RewriteEngine On
RewriteRule ^/([a-z0-9]+)  /load_user.php?user=$1 [NC,L,QSA]
webdestroya
Thanks for your help, im having trouble testing this (i have to wait) but it helps a lot.
pojomx
+7  A: 

Use mod_rewrite in your .htaccess file:

RewriteEngine on
RewriteOptions MaxRedirects=1
RewriteCond %{REQUEST_FILENAME} -f [NC,OR] 
RewriteCond %{REQUEST_FILENAME} -d [NC] 
RewriteRule .* - [L]
RewriteRule ^(.*)$ /foo.php/$1 [QSA,L]

Where foo.php is your script for showing the quick url pages.

Then in foo.php (or whatever you named it) you can catch the username in $_SERVER['PATH_INFO'].

The first 5 lines turn on mod_rewrite and allow actual files/folders/scripts to be ignored by the rewrite rule. The last one rewrites all the urls that aren't fines/folders/scripts.

Joshua
+1 for clear description.
Josh
Thank you, it helps a lot.
pojomx
Thanks, just to add:Mod Rewrite, should be ON (LoadModule rewrite_module modules/mod_rewrite.so, on httpd.conf)
pojomx
+1  A: 

You need to ask the webserver to rewrite that url to a query string with data that you can access from PHP. Do your PHP script to work with urls like

http://www.someniceandreliableurl.com/?user=username

Then ensure that apache have mod_rewrite installed and that you are allowed to use .htaccess. Then create a .htaccess file with a rewrite rule, like

RewriteEngine On
RewriteRule   ^/(.*)$ /?user=$1 [QSA]
Johan
Why the downvote?
musicfreak
@Joshua: Not a reason to downvote, IMO. The answer is naturally going to be at the bottom of the list anyway, because the other two answers were here first and thus got a head start. It's still a valid answer; no need to punish @Johan for being a slower typist.
musicfreak
A: 

Checkout ModRewrite in Apache or use a php framework that offers routing. This is generally based on the $ENV['PHP_SELF'] variable which lets you do much more in your php code that you would normally have to do in ModRewrite. It will return the part of the url after the hostname so you can do urls like http://exmaple.com/index.php/username/profile.

Jakub Hampl