Home > Backend Development > PHP Tutorial > Using Apache's rewrite technology_PHP tutorial

Using Apache's rewrite technology_PHP tutorial

WBOY
Release: 2016-07-21 16:11:57
Original
798 people have browsed it

使用Apache的rewrite技术

做PHP项目中需要用到URL重定向技术,基本上的需求就是把比如 /user/heiyeluren 重定向到 /user.php?uid=heiyeluren 之类的URL上,当然,你也可以把 /article/200707291011.html重定向到 /article.php?id=200507291011 之类的,模拟好像是静态页面,能够隐藏URL真实地址,有助于雨鞋基本的安全防范等等。那么好像rewrite是个很好的解决办法。

要在Apache里运行rewrite的话,必须先安装mod_rewrite的组件,就是一个mod_rewrite.c文件,然后必须在./configure的时候要放上mod_rewrite就能安装。
一般配置rewrite的话,可以在httpd.conf里面配置,也能在网页当前目录的.htaccess文件里进行定义来决定重定向去那个文件,那样的话,就非常具有灵活性了,同样也能够适合虚拟主机用户来做。

我们看一个.htaccess文件的例子:


1
2 RewriteEngine On
3 RewriteBase /
4 RewriteCond %{REQUEST_FILENAME} -f [OR]
5 RewriteCond %{REQUEST_FILENAME} -d
6 RewriteRule ^.*$ - [S=42]
7
8 #RewriteRule ^share/$ /share.php [QSA,L]
9 RewriteRule ^tag/([^/]+)/?$ /user_tags.php?tag=$1 [QSA,L]
10 RewriteRule ^city/([^/]+)/?$ /user_city.php?tag=$1 [QSA,L]
11 #RewriteRule ^([^/]+)/day/([^/]+)/?$ /user_share.php?id=$1&s=1&selTime=$2 [QSA,L]
12 #RewriteRule ^([^/]+)/day/([^/]+)/?$ /user_share.php?id=$1&s=1&selTime=$2 [QSA,L]
13
14 RewriteRule ^([^/]+)/day/([^/]+)/?$ /user_share.php?id=$1&s=1&selTime=$2 [QSA,L]
15 RewriteRule ^([^/]+)/week/([^/]+)/?$ /user_share.php?id=$1&s=2&selTime=$2 [QSA,L]
16 RewRiteRule ^([^/]+)/month/([^/]+)/?$ /user_share.php?id=$1&s=3&selTime=$2 [QSA,L]
17
18 RewriteRule ^([^/]+)/day/?$ /user_share.php?id=$1&s=1    [QSA,L]
19 RewriteRule ^([^/]+)/week/?$ /user_share.php?id=$1&s=2   [QSA,L]
20 RewriteRule ^([^/]+)/month/?$ /user_share.php?id=$1&s=3  [QSA,L]
21
22 RewriteRule ^([^/]+)/?$ /user_share.php?id=$1    [QSA,L]
23

比较长吧,我们就简单关注一下关键的内容。之间就是定义的内容,RewriteEngine就是确定是否运行URL重写功能,RewriteBase就是基本的路径是什么,最关键的就是下面的RewriteRule,就是我们需要重写的规则了,这里应用了兼容Perl规则的正则表达式:


Text:
  .           匹配任意单个字符
  [chars]     匹配当前字符
  [^chars]    不匹配当前字符
  text1|text2 包含text1或者text2任何一个

Quantifiers:
  ?           零个或者一个?号前的字符
  *           零个或者任意个任意长度的任意字符
  +           一个或者任意个任意长度的字符

Grouping:
  (text)      Grouping of text
              (either to set the borders of an alternative or
              for making backreferences where the Nth group can
              be used on the RHS of a RewriteRule with $N)

Anchors:
  ^           匹配内容开始标记
  $           匹配内容结束标记

Escaping:
  \char       使用\来进行特殊字符的转义,包括".[]()"等字符的转义


基本规则如上,比如下面的正则表达式:^/([^/]+)/~([^/]+)/(.*)$ 就是能够匹配象 /Language/~ Realname/.../File 这样的路径。

那么从这个角度去看上面的内容就比较容易理解了。我们简单看一下RewriteRule指令的规则:
RewriteRule   访问的路径    需要指向的真实路径
这样就很清楚了,比如说我要把/user/heiyeluren重定向到/user.php?uid=heiyeluren那么我的规则就必须这样:
RewriteRule ^user/([^/]+)$  ^/user.php?uid=$2 [QSA,L]

举一反三,就很容易理解规则如何去写,也就能够理解上面规则脚本的内容了。      

没有清楚的,请参考附上的链接,更深入的了解具体内容。写的不好请见谅。


Attachment:
Apache Rewrite technology http://www.yujs.com/recommendation/004.htm
Apache module mod_rewrite http://linux.dalouis.com/doc/apache2.0 /mod/mod_rewrite.html
URL Rewrite Guide http://linux.dalouis.com/doc/apache2.0/misc/rewriteguide.html
Apache HTTP Server 2.0 Version Document http://linux.dalouis .com/doc/apache2.0/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313764.htmlTechArticleUsing Apache's rewrite technology to do PHP projects requires URL redirection technology. The basic requirement is to use, for example, /user/heiyeluren redirects to a URL such as /user.php?uid=heiyeluren,...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template