您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Apache的URL地址重写

2026/1/15 19:40:44发布38次查看
第一种方法:apache环境中如果要将url地址重写,正则表达式是最基本的要求,但对于一般的url地址来说,基本的匹配就能实现我们大部分要求,因此除非是非常特殊的url地址,但这不是我要讨论的范围,简单几招学会apache中url地址重写,通过实例展示,轻松学会url地址重写:    
002    
003    url实例    
004    
005    重写url:http://www.baidu.com/?p=152    
006    
007    原始url:http://www.baidu.com/p152.html    
008    
009    重写规则:    
010    
011    ^p([0-9]+)\.html      /?p=$1     [l]    
012    
013    正则基础知识:    
014    
015    ^ 匹配行的开始,匹配url地址的开头部分,对于rewriterule而言,域名(http://www.xxxx.com)不是url地址的一部分,如上:?p=152    
016    
017    () 分隔一个被捕获的表达式,如上:([0-9]+)    
018    
019    [] 定义字符类,如上:[0-9] 表示从0-9的数字    
020    
021    + 说明前面的字符可以被重复匹配1次或数次,如上:[0-9]+,表示任何数字组合    
022    
023    \ 字符转义,如上:转义.    
024    
025    其它:    
026    
027    [l] 表示last,停止匹配其它    
028    
029    方法如下:    
030    
031    1,打开httpd.conf文件,找到    
032    
033    #loadmodule rewrite_module modules/mod_rewrite.so 注释前面#    
034    
035    2,打开httpd-vhosts.conf文件,在virtualhost添加重写规则,    
036    
037    rewriteengine on    
038    
039    rewriterule ^p([0-9]+)\.html      /?p=$1     [l]    
040    
041    基本上就上面这两个步骤,其实总的来说,apache中url地址重写还是比较简单的,比看文档学习要快的多,不过要想深入了解还是有必要看看相关文档的,其它规则可以自定义。记住一点:任何匹配其实就是一个正则表达式的替换过程。    
042    
043    创建友好的搜索引擎url地址对于php程序员来说非常重要,因此简单学会apache中url地址重写将是一项最基本的要求。    
044    
045    
046    第二种方法:    
047    
048    1,首先检查是否已安装rewrite模块:    
049    
050    cat httpd.conf | grep rewrite    
051    loadmodule rewrite_module modules/mod_rewrite.so    
052    
053    2,生成伪静态html连接:    
054    
055    (1)生成伪静态html    
056    
057    在<virtualhost>段最后加入    
058    
059    rewriteengine on    
060    rewriterule /goods([0-9]+).html /goods.php?id=$1 [pt]    
061    更标准的写法为:    
062    rewriterule ^(.*)/goods([0-9]+).html$ $1/goods.php?id=$2 [pt]    
063    更简洁的写法:    
064    /goods(\d+)\.html /goods\.php\?id=$1    
065    
066    第一个(0-9]+)对应参数$1,以此类推第二个对应$2    
067    
068    举例:    
069    
070    rewriterule /forum-([0-9]+)-([0-9]+)\.html /forumdisplay.php?fid=$1&page=$2 [pt]    
071    
072    测试http://www.xxx.com/goods1.html 是否与/goods.php?id=1的内容相同    
073    
074    最后将所有链接换成设置后的伪静态html地址方式    
075    
076    
077    [pt]:url全局转换,即转换过的goods31.html对应goods.php?id=31 (默认就是这个不加参数)    
078    [r]:    url重定向即使用goods31.html访问时跳转到goods.php?id=31    
079    
080    
081    3,防盗链:    
082    
083    rewritecrond %{http_host} !xxxx.com [r=301,l]    
084    rewriterule ^(.*)$ http://www.xxxx.com/warning.html [r=301,l]    
085    
086    把不是来自xxxx.com的请求重定向到http://www.xxxx.com    
087    
088    更好的做法:    
089    rewritecond %{http_referer} !^http://(www\.)?xxxx\.com/.*$ [nc]    
090    rewriterule \.(mp3|rar|jpe|gif)$ http://www.xxxx.com/warning.jpg [r=301,l]    
091    
092    4,防百度爬虫:    
093    rewritecond %{http_user_agent} ^baiduspider [or]    
094    rewriterule ^(.*)$ http://www.google.com [r=301,l]    
095    把来自百度的爬虫转到goole    
096    
097    
098    
099    
100    ps:php伪静态方式    
101    
102    方法一:    
103    
104    比如这个网页    
105    
106    http://www.xxxx.com/soft.php/1,100,8630.html    
107    
108    其实处理的脚本是soft.php 参数为1,100,8630    
109    
110    相当于soft.php?a=1&b=1=100&c=8630 只不过这样的url太难记。搜索引擎也不喜欢。    
111    
112    真静态只是完全生成了html。    
113    
114    客户端访问的时候直接输出。不用脚本解释。在流量非常大的时候(比如每天有上百万的访问量的时候)会起到很好的效果。也就是说服务器端实实在在的存在这个html页面。    
115    
116    当然在你网站的流量没有那么大的时候。url重写是最好的方法(个人观点,大流量的时候可以考虑负载均衡了。同样没有关系)    
117    
118    附url重写的方法有很多种,apache,iisrewrite。甚至php脚本都可以直接处理。比如上例中就是php脚本直接处理(该方法好处是大流量的时候直接减轻web伺服器的压力。ps:同样也是个人观点:    
119    
120    ================================================    
121    
122    下面以程序为例讲一下php伪静态的程序实现方法,其实这方法我之前已经有在其它论坛社区发过    
123    
124    程序为例:    
125    
126    http://www.xxxx.com/soft.php/1,100,8630.html    
127    
128    code:    
129    
130    //利用server变量 取得path_info信息 该例中为 /1,100,8630.html 也就是执行脚本名后面的部分    
131    
132    if(@$path_info =$ _server[path_info]){    
133    //正则匹配一下参数    
134    if(preg_match(/\/(\d+),(\d+),(\d+)\.html/si,$path_info,$arr_path)){    
135    $gid =intval($arr_path[1]); //取得值 1    
136    $sid =intval($arr_path[2]); //取得值100    
137    $softid =intval($arr_path[3]); //取得值8630    
138    }else die(path:error!);    
139    //相当于soft.php?gid=1&sid=100&softid=8630    
140    //就是这么简单了。~)    
141    方法二:    
142    一 打开 apache 的配置文件 httpd.conf 。    
143    二 将#loadmodule rewrite_module modules/mod_rewrite前面的#去掉    
144    三 在 httpd.conf中添加:    
145    <ifmodule mod_rewrite.c>    
146    rewriteengine on    
147    #rewritecond %{env:script_url} (?:index|dispbbs)[-0-9]+.html    
148    rewriterule ^(.*?(?:index|dispbbs))-([-0-9]+).html 1.php?__is_apache_rewrite=1&__rewrite_arg=2    
149    </ifmodule>    
150    四 要实现asp帖子url到php帖子的映射,在 第三步的<ifmodule mod_rewrite.c>和</ifmodule>之间添加:    
151    rewritemap tolowercase int:tolower    
152    rewritecond %{query_string} (?:boardid|page|id|replyid|star|skin)=d+ [nc]    
153    rewriterule ^(.*(?:index|dispbbs)).asp 1.php?{tolowercase:%{query_string}}&__is_apache_rewrite=1    
154    五 保存httpd.conf并重启apache    
155    方法三:    
156    <?php
157 function mod_rewrite(){
158 global
159 $ _get;
160 $nav=$ _server["request_uri"];
161 $script_name=$ _server["script_name"];
162 $nav=substr(ereg_replace("^$script_name","",urldecode($nav)),1);
163 $nav=preg_replace("/^.ht(m){1}(l){0,1}$/","",$nav);//这句是去掉尾部的.html或.htm
164 $vars = explode("/",$nav);
165 for($i=0;$i<count($vars);$i+=2){
166 $ _get["$vars[$i]"]=$vars[$i+1];
167 }
168 return $ _get;
169 }
170 mod_rewrite();
171 $yearn=$ _get["year"];//结果为'2006'
172 $action=$ _get["action"];//结果为'_add'
173 echo $yearn;
174 echo $action;
175 ?>    
176     177    function mod_rewrite(){    
178    global $ _get;    
179    $nav= $ _server[request_uri];    
180    $script_name= $ _server[script_name];    
181    $nav=substr(ereg_replace(^$script_name,,urldecode($nav)),1);    
182    $nav=preg_replace(/^.ht(m){1}(l){0,1}$/,,$nav);//这句是去掉尾部的.html或.htm    
183    $vars = explode(/,$nav);    
184    for($i=0;$i 185    $ _get[$vars[$i]]=$vars[$i+1];    
186    }    
187    return    
188    $ _get;    
189    }    
190    mod_rewrite();    
191    $yearn= $ _get[year];//结果为'2006'    
192    $action=$ _get[action];//结果为'_add'    
193    echo $yearn;    
194    echo $action;    
195    很多情况下,某个 ip 的访问很容易造成 cpu 100% (比如某些搜索引擎的固定抓取,别人大量的采集站点),这个时候我们就要利用一些有效的手段封掉对方的 ip,让他无法消耗服务器的资源,封 ip 的方法有很多种,如果你的 web 服务器安装了 rewrite 模块的话,也可以试试利用 rewrite 规则封掉对方的 ip。    
196    1、例如我们把某个特定的 ip 直接重定向到 baidu 首页,在网站根目录的 .htaccess 文件里添加代码:    
197    rewritecond % 123.123.123.123 [nc]rewriterule ^(.*)$ http://www.baidu.com/$1 [r=301] 将 123.123.123.123 这个 ip 替换成您要限制的 ip 即可    
198    2、如果要实现多个 ip ,可以这样写:    
199    rewritecond % 123.123.123.123 [or]rewritecond % 124.124.124.124 [nc]rewriterule ^(.*)$ http://www.baidu.com/$1 [r=301]    
 以上就是apache的url地址重写的内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product