一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

apache rewrite把php伪静态html页面例子

时间:2014-07-24 编辑:简简单单 来源:一聚教程网

apache rewrite 规则中有两个常用的标记 [L] 和 [QSA],要用$_GET获取伪静态后?后面的参数值就要用[QSA]标示。

[L] 和 [QSA] 的具体区别如下:

1)'last|L' (最后一个规则 last) 立即停止重写操作,并不再应用其他重写规则。
 
2)'qsappend|QSA' (追加请求串 query string append)

此标记强制重写引擎在已有的替换串中追加一个请求串,而不是简单的替换。 如果需要通过重写规则在请求串中增加信息,就可以使用这个标记。
 
例子
 

 代码如下 复制代码
对于伪静态之后 的地址/main/test?page=1
 
RewriteRule ^main/([a-zA-Z0-9]+)$ /index.php?c=main&m=index&a=$1 [L]
 
$_GET['page'] 获取不到
 
RewriteRule ^main/([a-zA-Z0-9]+)$ /index.php?c=main&m=index&a=$1 [QSA]
 
$_GET['page'] 可以获取到

例子

 代码如下 复制代码


    RewriteEngine on
    RewriteRule index.html$ index.php
    RewriteRule index-([1-9]+[0-9]*).html$ index.php?p=$1
    RewriteRule ([a-z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2

例子所在项目为test

在项目下 index.php 页面内写入内容

 代码如下 复制代码

if ($_GET ['p']) {
    echo "p : " . $_GET ['p'];
}

if ($_GET ['action']) {
    echo "action : " . $_GET ['action'];
}

if ($_GET ['id']) {
    echo "id : " . $_GET ['id'];
}
?>

在浏览器中输入

http://localhost/test/index.html
http://localhost/test/index-99.html
http://localhost/test/page-18.html

都会转向 http://localhost/test/index.php 页面

并且依次
http://localhost/test/index.html     页面什么都不显示
http://localhost/test/index-99.html  页面显示 p : 99
http://localhost/test/page-18.html   页面显示 action : pageid : 18

伪静态除了简单的配置之外还可以实现防盗链了,我们只要简单的配置一下即可这个作用也蛮大的哦。

如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容

 代码如下 复制代码
RewriteEngine on
#开启Rewrite模块
RewriteCond %{HTTP_REFERER} !^$
#如果不是直接输入图片地址
RewriteCond %{HTTP_REFERER} !img.你的域名$ [NC]
RewriteCond %{HTTP_REFERER} !google.com [NC]
RewriteRule (.*)\.(jpg|jpeg|jpe|gif|bmp|png|wma|mp3|wav|avi|mp4|flv|swf)$ http://你的域名/err.jpg [R=301,L,NC]

#截获所有.jpg或.jpeg……请求,跳转到http://你的域名/err.jpg提示错误的图片,注:该图片不能在原域名下,也不能在该.htaccess文件有效控制的文件夹中

热门栏目