如何利用apache重写静态页面

2023-05-18    分类: 网站建设

本技巧重点介绍在apache中如何开启apache重写规则,并实例讲解了apache重写静态页面案例

具体方法如下:
apache配置文件httpd.conf中加载:LoadModule rewrite_module modules/mod_rewrite.so

两种方法重写
先进种:

RewriteEngine On
RewriteRule ^//content/(.*)_(.*)htm$ /test.php?q=$1&a=$2

介绍:这种方法,比如链接文件:test.php,这样链接的页面实际请求的页面是:test.php?q=2303&a=ok.前面的content是伪装的

例子:
a.html :
test.php
test.php :
echo $_GET["q"];//结果是2303
echo $_GET["a"];//结果是ok.(有一个.)
?>

第二种:
#这里的content就是要伪装的文件夹名称
ForceType application/x-httpd-php

介绍:这种方法实际意义上说不是伪装content文件夹,而是让服务器解释content文件,没有拓展名的content文件.

例子:
a.html :
test.php
有一个content的文件,没有拓展名
content :
$fields = explode('/', $_SERVER['REQUEST_URI']);
$id_pos = array_search('content', $fields);
if( $id_pos !== false ){
$c_id = $fields[ $id_pos + 1 ];
$a_id = $fields[ $id_pos + 2 ];
}
if( $a_id ){
$_GET['aid'] = $a_id;
$_GET['cid'] = $c_id;
require_once('showarticle.php');
}
?>
test.php :
echo $_GET['aid'];//结果是445
echo $_GET['cid'];//结果是0
?>

下面是一些值得学习的apache重写规则:
RewriteEngine On
RewriteRule index.html$ index.php
RewriteRule ^([a-zA-Z]+)/$ index.php?en=$1
RewriteRule ^([a-zA-Z]+)$ index.php?en=$1
RewriteRule ^([0-9]+)/.html$ view.php?id=$1
RewriteRule ^([0-9]+)_([a-zA-Z0-9]+)/.html$ view.php?id=$1&user=$2

RewriteRule ^/([0-9]+)/.htm$ /test.php?id=$1
RewriteRule ^/newsview-([0-9]+)_([0-9]+)_([0-9]+)_([a-z]+)/.htm$ /test.php?type=$1&id=$2&page=$3&user=$4
RewriteRule ^/test/.htm$ /test.php
#RewriteRule ^/(.*)/.htm$ /test.php?add=$1
RewriteRule new.html$ a.php
RewriteRule ^/content/([a-z]+)_([0-9]+)/.html$ /a.php?who=$1&$page=$2
RewriteRule ^/([0-9]?).html$ /a.php?who=$1

RewriteEngine On
RewriteRule index.html$ index.php
RewriteRule ^([0-9]+)/.html$ read.php?id=$1
RewriteRule ^([0-9]+)_([0-9]+)/.html$ read.php?id=$1&p=$2

RewriteRule industry.html$ info.php?type=1
RewriteRule design.html$ info.php?type=2
RewriteRule interview.html$ info.php?type=3
RewriteRule exhibition.html$ info.php?type=4
RewriteRule home.html$ info.php?type=5
RewriteRule help.html$ info.php?type=6
RewriteRule bbs.html$ info.php?type=7
RewriteRule 99cad.html$ info.php?type=8
RewriteRule recruitment.html$ info.php?type=9

RewriteRule index.html$ index.php
RewriteRule ^([0-9]+)/.html$ view.php?id=$1

RewriteRule index.html$ index.php
RewriteRule ^([0-9]+)/.html$ read.php?id=$1
RewriteRule ^([0-9]+)_([0-9]+)/.html$ read.php?id=$1&p=$2

RewriteRule industry.html$ info.php?type=1
RewriteRule design.html$ info.php?type=2
RewriteRule interview.html$ info.php?type=3
RewriteRule exhibition.html$ info.php?type=4
RewriteRule home.html$ info.php?type=5
RewriteRule help.html$ info.php?type=6
RewriteRule bbs.html$ info.php?type=7
RewriteRule d.html$ info.php?type=8
RewriteRule recruitment.html$ info.php?type=9

文章标题:如何利用apache重写静态页面
链接地址:https://www.cdcxhl.com/news/259971.html

成都网站建设公司_创新互联,为您提供营销型网站建设全网营销推广网页设计公司电子商务网站建设服务器托管

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

手机网站建设