使用PHP实现Url Rewriter(URL重写)
来源:互联网 发布:指尖网络是干什么的 编辑:程序博客网 时间:2024/05/17 03:41
OsPHP.COM.CN OSPHP.COm.CN项目主页: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"; 开源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]/{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.
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.
* 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
?>
- 使用PHP实现Url Rewriter(URL重写)
- asp.net url rewriter url重写
- url rewriter
- URL Rewriter
- 技巧/诀窍:rewriter在ASP.NET中重写URL
- 【微软官方】在 ASP.NET 中执行 URL 重写rewriter
- URL Rewriter(转载)
- M$ Free URL Rewriter
- 使用HttpHandler实现URL重写
- 使用HttpHandler实现URL重写
- 使用HttpHandler实现URL重写
- 使用HttpHandler实现URL重写
- 使用HttpHandler实现URL重写
- 使用HttpModule实现URL重写
- 使用HttpHandler实现URL重写
- 使用urlrewrite 实现URL重写
- 使用HttpModule实现多个域名分别绑定到子目录URL Rewriter Module
- php为静态实现之URL重写
- 对CSDN的外包栏目感到遗憾
- C#学习笔记 2007年12月17日
- 准备虚拟机和汇编语言编程环境
- 修改Eclipse的 workspace目录
- C#学习笔记 2007年12月18日
- 使用PHP实现Url Rewriter(URL重写)
- Informix SQL 函数说明
- 用MINGW编译GCC4.2.2指南
- J2EE架构分析
- 漫谈Ajax在.Net中的使用(转)
- 学习代码大全
- NAT原理与应用
- Linux下使用oracle的经验总结
- XMLHTTP应用参考(转)