伪静态==rewrite重写
网站做搜索引擎优化,但是内容实时性比较强不能做静态页面引出可以通过rewrite重写来实现伪静态的效果。
定义:网站对外使用静态化的链接,而内部实际上仍然使用动态页面的URL形式。
案例介绍
Apache中的Mod_rewrite模块提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求
伪静态实现方式分两种:服务器级和目录级
伪静态配置方式:
httpd.conf 服务器级配置
# LoadModule rewrite_module modules/mod_rewrite.so
去除前面的 #
LoadModule rewrite_module modules/mod_rewrite.so
.htaccess 目录级配置
首先将虚拟目录中<Directory>的 AllowOverride None
修改为: AllowOverride All,让其加载根目录下的.htaccess文件,
mod_rewrite 规则的使用:
<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>
RedirectMatch ^(.*)/([^/]*)\.html$ $1/index.php?class=$2
RewriteRule 和 RedirectMatch区别
前者在具体内容时候显示时url地址不会发生跳转,后者url地址会跳转到真实的地址
上面中的 RedirectMatch
后面跟的正则表达式会匹配 http://www.phpfive.com/0809java.html
然后跳转到地址为 http://www.phpfive.com/index.php?class=0809java
RewriteRule index.html index.php
RewriteRule (\d+).html$ info\.php\?infoid=$1
www.phpfive.com/info/info.php?infoid=100 映射为 www.phpfive.com/info/100.html
RewriteRule p([0-9]+)\.html index\.php\?p=$1 [L]
www.phpfive.com/p1119.html 映射为 www.phpfive.com/?p=1119
RewriteRule article-([0-9]+)(.*).html article.php?id=$2 [QSA,L]
RewriteRule ^(.*)/article-([0-9]+)(.*).html$ $1/article.php?id=$2 [QSA,L]
www.phpfive.com/article-144.html 映射为 www.phpfive.com/article.php?id=144
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
mod_rewrite 规则修正符:
1) R 强制外部重定向
2) F 禁用URL,返回403HTTP状态码。
3) G 强制URL为GONE,返回410HTTP状态码。
4) P 强制使用代理转发。
5) L 表明当前规则是最后一条规则,停止分析以后规则的重写。
6) N 重新从第一条规则开始运行重写过程。
7) C 与下一条规则关联
8) T=MIME-type(force MIME type) 强制MIME类型
9) NS 只用于不是内部子请求
10) NC 忽略正则中的大小写
11) QSA 追加请求字符串
12) NE 不在输出转义特殊字符 \%3d$1 等价于 =$1