PHP Classes

Url Rewriter: Redirect requests according URL rewriting rules

Recommend this page to a friend!
  Info   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum (3)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 60%Total: 4,620 All time: 606 This week: 225Up
Version License PHP version Categories
url_rewriter 1.0.0BSD License3.0HTTP
Description Author

This class can be used to redirect requests based on URL rewriting rules.

It can check whether the current request URL matches one or more rules.

If one of the defined rules is matched, the class determines the path of a PHP script that will handle the request and executes that script using the PHP require command.

If none of the rules matches, the class include the default error script and returns the 404 status code to the browser.

Picture of Cesar D. Rodas
  Performance   Level  
Name: Cesar D. Rodas is available for providing paid consulting. Contact Cesar D. Rodas .
Classes: 38 packages by
Country: Paraguay Paraguay
Age: 35
All time rank: 71 in Paraguay Paraguay
Week rank: 557 Down1 in Paraguay Paraguay Equal
Innovation award
Innovation award
Nominee: 25x

Winner: 5x

URL Rewriter
Cesar D. Rodas <>

This class provides an easy mechanism to manage URL rewriting, avoiding to write and manage the  ".htaccess" file.
Another issue is that you can use a personalized regular expression for create friendlies URL.

I. Basic Structure
        require "url_rewriter.php";
        $url = new url_rewriter('example/');
        $url->e404 = "error.php"; 
        // regular expression rules goes here

II. Regular Expressions
    The regular expression are simple a set of constants, variable, and optionals parts.
        $url->add_rule("index.php","/[country]/{index.html}", array('country'=> $country) ); 
    If the first rule give true, "index.php" will be executed. For that country must be "Argentina", "Brasil" or "Paraguay" (this is because we force with the third parameter. "Country" must be in $country), and must exist an "index.html" or nothing.
        * /Paraguay/index.html  : Match
        * /Paraguay/                  : Match
        * /Paraguay/foo.html     : Mismatch
    The :numberX, where X is an identification, variable is an special variable that it must be numeric for match.
        $url->add_rule("foo.php","/[country]/[str]-[:number1]-site{-[:number2]}.[ext]", array('country'=> $country,':number2' => 2) );
        * /Paraguay/some_string-123-site.html                       :    Match
        * /Paraguay/some_other_string-123-site-456.html     :    Mismatch
        * /Paraguay/some_other_string-123-site-2.html         :    Match
        * /Paraguay/some_other_string-123-site-foo.html      :    Mismatch

III. Real life example
    Steps for  translate a page which is "foo.php?id=45" to its URL friendly mode.
    *   Download
    *   Edit url_handler.php
    *   Add a new rule
    *   Change all your links
        foo.php?id=45 for /entry-45.html
    * Edit foo.php adding an extra header
            $id = $urlVars['number1'];
            //check if $id exist
             if ($id not exist) $urlHandler->error404();
            //handle request as you did before
  Files folder image Files  
File Role Description
Files folder imageexample-urlrewriter (2 files)
Accessible without login Plain text file .htaccess Data Apache Regular expression
Accessible without login Plain text file readme.txt Doc. Documentation
Accessible without login Plain text file url_handler.php Example Example page
Plain text file url_rewriter.php Class Main class

  Files folder image Files  /  example-urlrewriter  
File Role Description
  Accessible without login Plain text file error.php Example Example page
  Accessible without login Plain text file index.php Example Example page

 Version Control Reuses Unique User Downloads Download Rankings  
This week:0
All time:606
This week:225Up
User Ratings User Comments (2)
 All time
For more information send a message to info at phpclasses dot org.