`
yangyangmilan
  • 浏览: 29844 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Apache的Mod_rewrite 例子(转)

阅读更多

转自dreamwaver的博客([url]http://dreamwaver.bokee.com/5692845.html[/url])

目标 重写设置 说明
规范化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 ^/(.*) [url]http://fully.qualified.domain.name:%[/url]{SERVER_PORT}/$1 [L,R] 重写
  RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC]  
  RewriteCond %{HTTP_HOST} !^$  
  RewriteRule ^/(.*) [url]http://fully.qualified.domain.name/$1[/url] [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/([^/]+)/?(.*) [url]http://$[/url]{user-to-host:$1|server0}/u/$1/$2 用户均衡
  RewriteRule ^/g/([^/]+)/?(.*) [url]http://$[/url]{group-to-host:$1|server0}/g/$1/$2 组均衡
  RewriteRule ^/e/([^/]+)/?(.*) [url]http://$[/url]{entity-to-host:$1|server0}/e/$1/$2 实体均衡
  RewriteRule ^/([uge])/([^/]+)/?$ /$1/$2/.www/  
  RewriteRule ^/([uge])/([^/]+)/([^.]+.+) /$1/$2/.www/$3\  
     
URL根目录搬迁 RewriteEngine on  
  RewriteRule ^/~(.+) [url]http://newserver/~$1[/url] [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 ^(/~.+) [url]http://www.somewhere.com/$1[/url] [R,L]  
     
错误重定向 RewriteEngine on  
  RewriteCond /your/docroot/%{REQUEST_FILENAME} !-f 不是regular文件
  RewriteRule ^(.+) [url]http://webserverB.dom/$1[/url]  
     
程序处理特殊协议 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/(.*)$ [url]http://www.tstimpreso.com/hotsheet/$1[/url] [P] 利用了代理模块
     
  RewriteEngine on  
  RewriteBase /~quux/  
  RewriteRule ^usa-news\.html$ [url]http://www.quux-corp.com/news/index.html[/url] [P]  
     
反向动态镜像 RewriteEngine on  
  RewriteCond /mirror/of/remotesite/$1 -U  
  RewriteRule ^[url]http://www[/url]\.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 (<STDIN>) {  
  $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} !^[url]http://www.quux-corp.de/~quux/.[/url]*$ [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
分享到:
评论

相关推荐

    Apache mod_rewrite中的REQUEST_URI使用实例

    Apache mod_rewrite 是一个强大的URL重写模块,常用于优化网站的URL结构,实现SEO友好、隐藏实际路径或处理无效请求。在本实例中,我们将深入理解如何使用REQUEST_URI这一关键变量,以及如何解决在URL重写过程中遇到...

    apache rewrite_module模块使用教程

    本文将详细介绍如何使用`mod_rewrite`模块,并提供实际应用的例子。 #### 二、`mod_rewrite`模块简介 `mod_rewrite`是Apache服务器中的一个可选模块,用于对HTTP请求进行URL转换。该模块可以通过一系列预定义的...

    apache mod_ruby

    3. **Ruby重写规则**:结合使用mod_rewrite,你可以创建复杂的URL重写规则,将用户友好的URL映射到Ruby脚本。 4. **部署Web应用**:对于更复杂的应用,如Ruby on Rails,mod_ruby可以与DispatcherGem配合,使Apache...

    Apache Rewrite Module 的重定向问题

    ### Apache Rewrite Module 重定向问题解析 #### 一、Apache Rewrite Module 概述 Apache的Rewrite Module(重写模块)是Apache服务器中一个非常强大的工具,主要用于URL重写及重定向功能。它通过一系列规则对请求...

    UrlRewrite例子

    首先,UrlRewrite是Tuckey开源组织提供的一款过滤器,它基于Apache的开源项目 mod_rewrite 的思想,实现了在Servlet容器(如Tomcat)中的URL重写功能。通过在web.xml中配置UrlRewriteFilter,我们可以定义一系列规则...

    解析 .htaccess 文件 apache rewrite

    模块`mod_rewrite`是Apache的一个核心模块,它提供了URL重写和URL路由的能力,这对于创建SEO友好的伪静态URL、实现URL短链、隐藏真实路径等场景非常有用。在`.htaccess`文件中启用`mod_rewrite`模块,你需要开启...

    iis+zendframework

    ZendFramework的控制器可以被设置支持使用clean urls,这点在apache下很好写,前提是apache支持mod_rewrite模块,然后通过设置.htaccess可以对ZendFramework单独控制.但是在IIS写如何设置呢? 问题很简单,通过...

    PHP伪静态Rewrite设置之APACHE篇

    在Apache的主配置文件`httpd.conf`中,你需要确保`LoadModule rewrite_module modules/mod_rewrite.so`这行已存在并取消注释,这行命令用于加载Rewrite模块。此外,如果需要在特定目录下启用`.htaccess`文件以进行...

    Apache伪静态Rewrite详解

    5. **Apache mod_rewrite例子** - 示例一:当请求的URL是//www.jb51.net/xxx.php时,实际上访问的是//www.jb51.net/xxx/;当请求的域名是http://yyy.jb51.net时,实际访问的是//www.jb51.net/user.php?username=yyy...

    urlrewrite简单例子

    "urlrewrite"是一个非常实用的工具,它基于Apache服务器的URL重写模块,可以帮助开发者轻松实现动态URL到静态URL的转换。本篇文章将深入探讨如何使用urlrewrite实现动态页面静态化,并通过具体的例子来阐述这一过程...

    apache虚拟目录和伪静态的配置

    要在Apache中启用伪静态,首先需要确保`mod_rewrite`模块已加载,然后在相应配置文件(如`.htaccess`)中添加规则。下面是一个简单的伪静态配置示例,用于处理常见的WordPress permalinks: ```apacheconf ...

    php伪静态例子

    实际上,伪静态并不改变网页的动态本质,而是通过URL重写技术(比如Apache的mod_rewrite或Nginx的rewrite规则)将动态URL映射为看起来像静态页面的URL。这样,用户和搜索引擎看到的是静态化的地址,但服务器依然处理...

    Apache的Rewrite心得

    如果未安装,可以在编译Apache时添加`--enable-mods-shared=all --enable-rewrite`选项,或者在已安装的Apache上使用包管理器(如apt-get或yum)安装`mod_rewrite`模块。在配置文件(通常为httpd.conf或apache2.conf...

    Apache负载均衡器配置

    - **编译Apache**:通过`./configure`脚本进行配置,这里指定了多个模块,如`mod_so`、`mod_setenvif`、`mod_rewrite`等。这些模块对于实现负载均衡功能至关重要。编译完成后,执行`make`和`make install`完成安装...

    IIS rewrite

    IIS Rewrite 模块是基于Apache mod_rewrite的一个类似实现,它允许服务器根据预定义的规则对HTTP请求的URL进行重写。这些规则通常以正则表达式的形式编写,使得可以实现复杂的URL转换逻辑。IIS Rewrite不仅支持URL...

    url rewrite的例子

    Apache HTTP Server 2.x 和 mod_proxy提供了不错的Rewrite处理方法。so如果使用apache,则直接在apache中配置Rewrite即可。但是对于没有使用代理,或者不完全要在代理服务器配置Rewrite的web应用,通过第三方Jar包...

    如何整合apache和tomcat

    在这个例子中,我们使用的是Apache 2.2.3的Windows版本(apache_2.2.3-win32-x86-no_ssl.msi)。安装过程中,确保选择合适的配置选项,以便在需要时能够修改配置文件。 接着,我们需要下载并配置mod_jk模块。mod_jk...

    伪静态,利用urlrewritel重写url例子

    首先,我们需要在Apache服务器上安装并启用`mod_rewrite`模块。在`httpd.conf`或`.htaccess`配置文件中找到以下行并取消注释: ```apache LoadModule rewrite_module modules/mod_rewrite.so ``` 然后,为了在站点...

    urlrewrite静态页面的使用例子

    `urlrewrite`是基于Apache的`mod_rewrite`模块设计的一个Java实现,它允许我们在Java Web应用中实现类似于`.htaccess`文件的功能。通过定义规则,我们可以将复杂的动态URL转换为简洁的静态形式,或者反之,将静态URL...

    apache 开启重定向 rewrite的实现方法

    在Windows环境中,你可能需要编辑`httpd.conf`文件,取消`LoadModule rewrite_module modules/mod_rewrite.so`行的注释,然后重启Apache服务。 3. **重启Apache服务**:完成上述操作后,重启Apache服务以应用更改...

Global site tag (gtag.js) - Google Analytics