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..
making pages display without extension? (simple question)
Moderator: General Moderators
-
xpiamchris
- Forum Newbie
- Posts: 10
- Joined: Thu Jan 15, 2009 4:17 pm
-
BornForCode
- Forum Contributor
- Posts: 147
- Joined: Mon Feb 11, 2008 1:56 am
Re: making pages display without extension? (simple question)
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)
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:
And php, incomplete:
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.)
Example Htaccess:
Code: Select all
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
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);
?>
[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)
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)
wow. thanks for the reply guys... i didn't know what the term was. i'll look into it and try it out.
thanks ^^
thanks ^^