Page 1 of 1

making pages display without extension? (simple question)

Posted: Thu Jul 02, 2009 2:24 am
by xpiamchris
how do sites like xanga/facebook make it so that they can display a site like this..


xanga.com/username <--vs xanga.com/username.php or xanga.com/userpage?user=141

always wondered about that..

Re: making pages display without extension? (simple question)

Posted: Thu Jul 02, 2009 2:32 am
by requinix
It's called "URL rewriting".

Re: making pages display without extension? (simple question)

Posted: Thu Jul 02, 2009 4:19 am
by BornForCode

Code: Select all

 
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1
The following example will rewrite the product.php?id=5 to porduct-5.html i.e when a URL like http://localhost/product-5.html calls product.php?id=5 automatically.
 

Re: making pages display without extension? (simple question)

Posted: Thu Jul 02, 2009 5:14 am
by SeaJones
A really straightforward way to approach this, assuming you're not using a URL handling system like the URLs structure in CodeIgniter etc., is to use htaccess to direct all traffic that isn't an existing file to index.php, and use index to handle the chopping up of the url and dealing with what to do with it.

Example Htaccess:

Code: Select all

 
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
 
And php, incomplete:

Code: Select all

 
<?php
 
$querystring = $_SERVER['QUERY_STRING'];
 
// Include some string sanitising code here to clean any nastiness out.
 
$pages = explode('/',$querystring);
 
print_r($pages);
 
?>
 
if these scripts were on example.com, at the server root, then a request to "http://example.com/about-me/phonenumbers/home" would leave you with an array in which the following values would have been applied:

[0] => "about-me"
[1] => "phonenumbers"
[2] => "home"

Which is easy to deal with.

*CAVEAT*

If you use this method, and many systems do (Wordpress for example, the htaccess is pretty much the same as theirs I'd imagine) then be careful with what is put into it. It's the equivalent of allow the user to set variables within your system, albeit variables within an array. If you were to use any of these variable as part of a SQL query, they'd be especially vulnerable, and you'd need to use some code to sanitise them carefully (I don't recommend relying on stripslashes and such for this, I'd use a regex to match troublesome crap.)

Re: making pages display without extension? (simple question)

Posted: Thu Jul 02, 2009 5:16 am
by SeaJones
Also, forgot to say, for simple systems, a good way to handle these urls is a switch-case set to deal with the possibles. :)

Re: making pages display without extension? (simple question)

Posted: Thu Jul 02, 2009 1:03 pm
by xpiamchris
wow. thanks for the reply guys... i didn't know what the term was. i'll look into it and try it out.

thanks ^^