Use PHP to implement URL rewriter (URL rewriting)

Source: Internet
Author: User

Project home: http://www.thyphp.com/friendly-url-is-it-really-needed.html
: Http://cesars.users.phpclasses.org/browse/package/4231.html

URL rewriter
Cesar D. Rodas <cesar@sixdegrees.com>

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
<? PHP
Require "Url_rewriter.php" ; Open-source osphp. com. cn
$ URL = New Url_rewriter ( 'Example /' );
$ URL -> E404 = "Error. php" ;
// Regular expression rules goes here
$ URL -> Execute ();
?>

Ii. Regular Expressions
The regular expression are simple a set of constants, variable, and optionals parts.
Example:
$ Country = array ('argentina ', 'brasil', 'paraguay ');
$ URL-> add_rule ("index. php", "/[country]/indexes 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.

Osphp. com. cn


*/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.

Example:
$ Country = array ('argentina ', 'brasil', 'paraguay ');
$ URL-> add_rule ("foo. PHP ","/[country]/[STR]-[: number1]-site {-[: number2]}. [ext] ", array ('country' => $ country, ': number2' => 2); osphp. com. CN

*/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.

Osphp. com. cn

* Download http://cesars.users.phpclasses.org/url_rewriter
* Edit url_handler.php
* Add a new rule
$ URL-> add_rule ("foo. php", "/entry-[: number1).html ");
* Change all your links
Foo. php? Id = 45 for/entry-45.html
* Edit Foo. php adding an extra Header
<? PHP
$ ID
= $ Urlvars [ 'Number1' ];
// Check if $ id exist
If ( $ Id not exist ) $ Urlhandler -> Error404 ();
// Handle request as you did before
?>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.