making pages display without extension? (simple question)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xpiamchris
Forum Newbie
Posts: 10
Joined: Thu Jan 15, 2009 4:17 pm

making pages display without extension? (simple question)

Post 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..
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

It's called "URL rewriting".
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

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

Post 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.
 
SeaJones
Forum Commoner
Posts: 48
Joined: Tue Jun 30, 2009 5:40 pm

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

Post 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.)
SeaJones
Forum Commoner
Posts: 48
Joined: Tue Jun 30, 2009 5:40 pm

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

Post 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. :)
xpiamchris
Forum Newbie
Posts: 10
Joined: Thu Jan 15, 2009 4:17 pm

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

Post 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 ^^
Post Reply