apache RewriteRule

apache RewriteRule最近发现一个新的东西(可能以前一直没有注意)Apache中的rewrite_modul模块。可以将你的url得到的地址直接重新定义成自己想要的位置。 也就是说输入的URL可以重新定义成一个动态的地址。而且这个URL 不会被改写,动态地址是在后台执行然后返回数据。这个功能还可以使用在写程序时自己定义的CACHE,完全提高服务器负担,加快响应时间,这个就看

大家好,又见面了,我是你们的朋友全栈君。

最近发现一个新的东西(可能以前一直没有注意) Apache 中的 rewrite_modul 模块。

可以将你的 url 得到的地址直接重新定义成 自己想要的位置。  也就是说 输入的 URL 可以 重新定义成 一个动态的 地址。 而且这个 URL 不会被改写,动态地址是在后台执行然后返回数据。

这个功能还可以使用在 写程序时自己定义的 CACHE ,完全提高服务器负担,加快响应时间,这个就看你怎么使用了。

取消了APACHE 配置文件 http.conf 里LoadModule rewrite_module modules/mod_rewrite.so前面的注释。目录下需要有一个配置文件 .htaccess

内容如下(例子):

RewriteEngine On
RewriteBase /tt/
RewriteCond %{ ENV:REDIRECT_SURI } !^$
RewriteRule (.*) – [L]
RewriteRule ^[[:alnum:]]+/*$ blog/index.php [E=SURI:1,L]
RewriteRule ^[[:alnum:]]+/+[0-9]+$ blog/item.php [E=SURI:1,L]
RewriteRule ^[[:alnum:]]+/+favicon/.ico$ blog/favicon.ico.php [E=SURI:1,L]
RewriteRule ^[[:alnum:]]+/+index/.gif$ blog/index.gif.php [E=SURI:1,L]
RewriteCond %{ QUERY_STRING } (^|&)pl=([0-9]+)
RewriteRule ^[[:alnum:]]+/+index/.php$ %2 [NE,L]
RewriteCond %{ REQUEST_FILENAME } -f [OR]
RewriteCond %{ REQUEST_FILENAME } -d 
RewriteRule !^(blog|cache)/ – [L]      
RewriteRule ^[[:alnum:]]+/+(entry|attachment|category|keylog|tag|search|plugin)/? blog/$1/index.php [E=SURI:1,L]
RewriteRule ^[[:alnum:]]+/+(.+)/[0-9]+$ blog/$1/item.php [E=SURI:1,L]
RewriteRule ^[[:alnum:]]+/+(.+)$ blog/$1/index.php [E=SURI:1,L]

具体内容需要按照自己使用时候不同而设置不同。同样上面的信息可以直接写进 Apache 的 conf 文件里面,用样有效。 如果生成文件后 就只对该目录有效。

Apache 官方的说明书 http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

这个是个很好的东西,还需要具体研究一下。

具体例子如下:

目标 重写设置 说明
规范化URL RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R] 将/~user重写为/u/user的形式
  RewriteRule ^/([uge])/([^/]+)$ /$1/$2/ [R] 将/u/user末尾漏掉的/补上
     
规范化HostName RewriteCond %{ HTTP_HOST } !^fully/.qualified/.domain/.name [NC] 域名不合格
  RewriteCond %{ HTTP_HOST } !^$ 不空
  RewriteCond %{ SERVER_PORT } !^80$ 不是80端口
  RewriteRule ^/(.*) http://fully.qualified.domain.name:%{ SERVER_PORT }/$1 [L,R] 重写
  RewriteCond %{ HTTP_HOST } !^fully/.qualified/.domain/.name [NC]  
  RewriteCond %{ HTTP_HOST } !^$  
  RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]  
     
URL根目录转移 RewriteEngine on  
  RewriteRule ^/$ /e/www/ [R] 从/移到/e/www/
     
末尾目录补斜线 RewriteEngine on  
(目录范围内) RewriteBase /~quux/  
  RewriteRule ^foo$ foo/ [R] /~quux/foo是一个目录,补/
     
  RewriteEngine on  
  RewriteBase /~quux/  
  RewriteCond %{ REQUEST_FILENAME } -d 如果请文件名是个目录
  RewriteRule ^(.+[^/])$ $1/ [R] URL末尾不是斜线时补上
     
Web集群 RewriteEngine on  
  RewriteMap user-to-host txt:/path/to/map.user-to-host 用户-服务器映射
  RewriteMap group-to-host txt:/path/to/map.group-to-host 组-服务器映射
  RewriteMap entity-to-host txt:/path/to/map.entity-to-host 实体-服务器映射
  RewriteRule ^/u/([^/]+)/?(.*) http://${ user-to-host:$1|server0 }/u/$1/$2 用户均衡
  RewriteRule ^/g/([^/]+)/?(.*) http://${ group-to-host:$1|server0 }/g/$1/$2 组均衡
  RewriteRule ^/e/([^/]+)/?(.*) http://${ entity-to-host:$1|server0 }/e/$1/$2 实体均衡
  RewriteRule ^/([uge])/([^/]+)/?$ /$1/$2/.www/  
  RewriteRule ^/([uge])/([^/]+)/([^.]+.+) /$1/$2/.www/$3/  
     
URL根目录搬迁 RewriteEngine on  
  RewriteRule ^/~(.+) http://newserver/~$1 [R,L] 到其它服务器
     
所用户名首字母分 RewriteEngine on  
  RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /home/$2/$1/.www$3 内一层括号为$2
     
NCSA imagemap移 RewriteEngine on  
植为mod_imap RewriteRule ^/cgi-bin/imagemap(.*) $1 [PT]  
     
多目录查找资源 RewriteEngine on  
  # first try to find it in custom/…  
  RewriteCond /your/docroot/dir1/%{ REQUEST_FILENAME } -f  
  RewriteRule ^(.+) /your/docroot/dir1/$1 [L]  
  # second try to find it in pub/…  
  RewriteCond /your/docroot/dir2/%{ REQUEST_FILENAME } -f  
  RewriteRule ^(.+) /your/docroot/dir2/$1 [L]  
  # else go on for other Alias or ScriptAlias directives,  
  RewriteRule ^(.+) – [PT]  
     
据URL设置环境变量 RewriteEngine on  
  RewriteRule ^(.*)/S=([^/]+)/(.*) $1/$3 [E=STATUS:$2]  
     
虚拟主机 RewriteEngine on  
  RewriteCond %{ HTTP_HOST } ^www/.[^.]+/.host/.com$ 基于用户名
  RewriteRule ^(.+) %{ HTTP_HOST }$1 [C]  
  RewriteRule ^www/.([^.]+)/.host/.com(.*) /home/$1$2  
     
内外人有别 RewriteEngine on  
  RewriteCond %{ REMOTE_HOST } !^.+/.ourdomain/.com$ 基于远程主机
  RewriteRule ^(/~.+) http://www.somewhere.com/$1 [R,L]  
     
错误重定向 RewriteEngine on  
  RewriteCond /your/docroot/%{ REQUEST_FILENAME } !-f 不是regular文件
  RewriteRule ^(.+) http://webserverB.dom/$1  
     
程序处理特殊协议 RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 / Xredirect协议
  [T=application/x-httpd-cgi,L]  
     
最近镜像下载 RewriteEngine on  
  RewriteMap multiplex txt:/path/to/map.cxan 顶级域名与最近ftp服务器映射
  RewriteRule ^/CxAN/(.*) %{ REMOTE_HOST }::$1 [C]  
  RewriteRule ^.+/.([a-zA-Z]+)::(.*)$ ${ multiplex:$1|ftp.default.dom }$2 [R,L] 据顶级域名不同提供不同的FTP服务器
     
基于时间重写 RewriteEngine on  
  RewriteCond %{ TIME_HOUR }%{ TIME_MIN } >0700  
  RewriteCond %{ TIME_HOUR }%{ TIME_MIN } <1900  
  RewriteRule ^foo/.html$ foo.day.html 白天为早晚7点间
  RewriteRule ^foo/.html$ foo.night.html 其余为夜间
     
向前兼容扩展名 RewriteEngine on  
  RewriteBase /~quux/  
  # parse out basename, but remember the fact  
  RewriteRule ^(.*)/.html$ $1 [C,E=WasHTML:yes]  
  # rewrite to document.phtml if exists  
  RewriteCond %{ REQUEST_FILENAME }.phtml -f 如果存在$1.phtml则重写
  RewriteRule ^(.*)$ $1.phtml [S=1]  
  # else reverse the previous basename cutout  
  RewriteCond %{ ENV:WasHTML } ^yes$ 如果不存在$1.phtml,则保持不变
  RewriteRule ^(.*)$ $1.html  
     
文件改名(目录级) RewriteEngine on 内部重写
  RewriteBase /~quux/  
  RewriteRule ^foo/.html$ bar.html  
     
  RewriteEngine on 重定向由客户端再次提交
  RewriteBase /~quux/  
  RewriteRule ^foo/.html$ bar.html [R]  
     
据浏览器类型重写 RewriteCond %{ HTTP_USER_AGENT } ^Mozilla/3.*  
  RewriteRule ^foo/.html$ foo.NS.html [L]  
  RewriteCond %{ HTTP_USER_AGENT } ^Lynx/.* [OR]  
  RewriteCond %{ HTTP_USER_AGENT } ^Mozilla/[12].*  
  RewriteRule ^foo/.html$ foo.20.html [L]  
  RewriteRule ^foo/.html$ foo.32.html [L]  
     
动态镜像远程资源 RewriteEngine on  
  RewriteBase /~quux/  
  RewriteRule ^hotsheet/(.*)$ http://www.tstimpreso.com/hotsheet/$1 [P] 利用了代理模块
     
  RewriteEngine on  
  RewriteBase /~quux/  
  RewriteRule ^usa-news/.html$ http://www.quux-corp.com/news/index.html [P]  
     
反向动态镜像 RewriteEngine on  
  RewriteCond /mirror/of/remotesite/$1 -U  
  RewriteRule ^http://www/.remotesite/.com/(.*)$ /mirror/of/remotesite/$1  
     
负载均衡 RewriteEngine on 利用代理实现round-robin效果
  RewriteMap lb prg:/path/to/lb.pl  
  RewriteRule ^/(.+)$ ${ lb:$1 } [P,L]  
     
  #!/path/to/perl  
  $| = 1;  
  $name = “www”; # the hostname base  
  $first = 1; # the first server (not 0 here, because 0 is myself)  
  $last = 5; # the last server in the round-robin  
  $domain = “foo.dom”; # the domainname  
  $cnt = 0;  
  while ( ) {  
  $cnt = (($cnt+1) % ($last+1-$first));  
  $server = sprintf(“%s%d.%s”, $name, $cnt+$first, $domain);  
  print “http://$server/$_”;  
  }  
  ##EOF##  
     
静态页面变脚本 RewriteEngine on  
  RewriteBase /~quux/  
  RewriteRule ^foo/.html$ foo.cgi [T=application/x-httpd-cgi]  
     
阻击机器人 RewriteCond %{ HTTP_USER_AGENT } ^NameOfBadRobot.*  
  RewriteCond %{ REMOTE_ADDR } ^123/.45/.67/.[8-9]$  
  RewriteRule ^/~quux/foo/arc/.+ – [F]  
     
阻止盗连你的图片 RewriteCond %{ HTTP_REFERER } !^$  
  RewriteCond %{ HTTP_REFERER } !^http://www.quux-corp.de/~quux/.*$ [NC] 自己的连接可不能被阻止
  RewriteRule .*/.gif$ – [F]  
     
  RewriteCond %{ HTTP_REFERER } !^$  
  RewriteCond %{ HTTP_REFERER } !.*/foo-with-gif/.html$  
  RewriteRule ^inlined-in-foo/.gif$ – [F]  
     
拒绝某些主机访问 RewriteEngine on  
  RewriteMap hosts-deny txt:/path/to/hosts.deny  
  RewriteCond ${ hosts-deny:%{ REMOTE_HOST }|NOT-FOUND } !=NOT-FOUND [OR]  
  RewriteCond ${ hosts-deny:%{ REMOTE_ADDR }|NOT-FOUND } !=NOT-FOUND  
  RewriteRule ^/.* – [F]  
     
用户授权 RewriteCond %{ REMOTE_IDENT }@%{ REMOTE_HOST } !^friend1@client1.quux-corp/.com$  
  RewriteCond %{ REMOTE_IDENT }@%{ REMOTE_HOST } !^friend2@client2.quux-corp/.com$  
  RewriteCond %{ REMOTE_IDENT }@%{ REMOTE_HOST } !^friend3@client3.quux-corp/.com$  
  RewriteRule ^/~quux/only-for-friends/ – [F]  
     
外部重写程序模板 RewriteEngine on  
  RewriteMap quux-map prg:/path/to/map.quux.pl  
  RewriteRule ^/~quux/(.*)$ /~quux/${ quux-map:$1 }  
     
  #!/path/to/perl  
  $| = 1;  
  while (<>) {  
  s|^foo/|bar/|;  
  print $_;  
  }  
     
搜索引擎友好 RewriteRule ^/products$ /content.php  
  RewriteRule ^/products/([0-9]+)$ /content.php?id=$1  
  RewriteRule ^/products/([0-9]+),([ad]*),([0-9]{ 0,3 }),([0-9]*),([0-9]*$) /marso/content.php?id=$1&sort=$2&order=$3&start=$4
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146378.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Pycharm 最新激活码 2021[免费获取]

    (Pycharm 最新激活码 2021)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlMLZPB5EL5Q-eyJsaWNlbnNlSWQi…

    2022年3月21日
    54
  • c# 字符串转时间的方式

    c# 字符串转时间的方式**第一种方法**stringtimeStr=”2019-08-28″;DateTimetime=Convert.ToDateTime(timeStr);**第二种:**DateTimeFormatInfotimeForInfo=newDateTimeFormatInfo();timeForInfo.ShortDatePattern=”yyyy/MM/dd”;stringtimeStr1=”2018-09-08″;vartime=Convert.

    2022年5月5日
    44
  • QT中this指针

    QT中this指针一:this指针只能在一个类的成员函数中调用,它表示当前对象的地址。下面是一个例子: void Date::setMonth( int mn) { month=mn; // 这三句是等价的 this->month=mn; (*this).month=mn; }1.this只能在成员函数中使用。全局函数,静态函数都不能使用th

    2022年5月17日
    143
  • ue4是什么意思_ue4主要是做什么用的「建议收藏」

    ue4是什么意思_ue4主要是做什么用的「建议收藏」ue4主要是做什么用的?UE4,即虚幻4,它是一套为开发实时技术而存在的引擎工具。目前广泛应用于3D建模渲染、游戏开发中。它完善的工具套件以及简易的工作流程能够使开发者快速修改或查看成果,对于代码的依赖性很低。而完整公开的源代码则能让使用者自由修改和扩展引擎功能。UE4开发引擎能够通过实时渲染,从NPC、人物角色、道具、AI等等方面很好地对游戏进行开发编辑。无论是关卡设计人员、美术开发人员、还是程…

    2022年10月5日
    2
  • Win10安装Tomcat服务器与配置环境变量[通俗易懂]

    Win10安装Tomcat服务器与配置环境变量[通俗易懂]文章目录下载安装JDK下载Tomcat压缩包解压Tomcat压缩包Tomcat目录结构启动Tomcat环境变量配置Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。本文主要讲述Windows环境Tomcat服务器安装与环境变量配置下载安装JDK要想安装Tomcat服务…

    2022年6月3日
    31
  • Linux文本处理神器awk实战案例

    Linux文本处理神器awk实战案例1.什么是awk2.打印不同列的内容3.打印行号和列号4.修改输入和输出分隔符5.输入多个文件6.修改某一列的值7.条件筛选后打印8.运算9.正则表达式

    2022年7月11日
    18

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号