samScripts.com
[Home] [My Scripts] [Contact] [Business Card Designer] [Donate] [$8.95 Domain Names]

Home > Scripts > friendlyurls

friendlyurls

... feel free to use this script for whatever you like.

friendlyurls

Very simple utility script to help you put nice looking links on your site instead of long query strings.

Description:

Introduction


This is a class to use together with apache <ForceType> directives (in .htaccess or httpd.conf) to give your site nice looking urls for dynamic pages.

Example


As an example, take the scripts section of this site. Instead of urls that look like:

http://www.samscripts.com/scripts.php?script=friendlyurls

it has urls like:

http://www.samscripts.com/scripts/friendlyurls.

For more information on how this is done, read the tutorials listed in the links section.

What friendlyurls does


All that this class does is get the parts of the url that aren't real, and puts them in a nice array that you can use as arguments or whatever in your script.

Samscripts.com Example



Quote from .htaccess file:

<Files scripts>
ForceType application/x-httpd-php
</Files>


These lines tell apache that it should treat the file called scripts as if it was a php file.

The scripts file:

<?php

include("friendlyurls.php"); // include the class

$ft = new friendlyurls("/[^\/]*(\.[^\/]*)/", "");

/*
the (optional) argument to the constructor is a regular expression telling the class to strip out
any parts of the url that are file extensions (eg .htm)

the friendlyurls object now contains two arrays:
$arguments, which contains all parts of the url after /scripts/
and $arguments_slashed which contains the same, but in a database safe addslashes() format

*/

$scripts = isset($ft->arguments_slashed[0]) ? $ft->arguments_slashed[0] : "";

// $scripts should now contain the name of the script to show info on

$scriptdetails = mysql_query("SELECT * FROM scripts WHERE title='$scripts'");

// display the script details

?>


I probably haven't explained this very well. Try asking in the support forum.

Reference


Constructor
$furl = new friendlyurl($search, $replace, $noduplicates = true, $noquerystring = true)
$search is a PCRE regular expression to search for in the parts of the url
$replace is what to replace them with
$noduplicates means that each keyword is only listed once,
eg: http://www.samscripts.com/scripts/cat/dog/cat/fish
would result in the $arguments array containing:
"cat", "dog", "fish" if $noduplicates was true
and "cat", "dog", "cat", "fish" if $noduplicates is false
$noquerystring means that any querystring is not included

variables
After the class is created, the following member variables can be accessed:
$arguments an array of all the words between /'s after the script name
$arguments_slashed the same array with addslashes() applied to each item
$arguments_count the number of items in the $arguments arrays

Files

friendlyurls.php (1699 bytes) View Download
Viewed 1020 times. Downloaded 1519 times.

[Back] [Top] [Forward]

Powered by php, mySql.

Request processed in 0.00 seconds on Wednesday 26th September 2012 23:43:10.

©2002 Sam Yapp

What do you think of my resume wizard site? All nicely written in php :)