apache url rewrite 的RewriteRule参数详解
apache url rewrite 的RewriteRule参数详解
我们经常会在apache那边对一些url进行一些重写操作,那么就会涉及到很多的重写策略的选择,比如重定向的方式,参数的处理,规则匹配的顺序等。
比如我们某个应用会有下面的url rewrite规则:下面是对于域名imall.test.com访问的时候需要重定向到http://page.test.com/channel/imall/index.html这个url上,
<VirtualHost *>
ServerName imall.test.com#if("${industry_port}"!="80"):${industry_port}#end
RewriteEngine on
RewriteCond %{HTTP_HOST} =imall.test.com [NC]
RewriteRule ^/$ http://page.test.com/channel/imall/index.html [L,R]
</VirtualHost>
之前是想上面这么写的,但是发现浏览器的url变了,也就是R其实是外部重定向,那么我希望浏览器的url不改变,那么就需要内部重定向或者反向代理的设计。此时只能参考官方文档了:http://httpd.apache.org/docs/2.2/rewrite/flags.html
这里列出很多的参数:在每条规则后面可以加上多个参数,每个参数用逗号分隔
RewriteRule pattern target [Flag1,Flag2,Flag3]
B (escape backreferences):非字母字符会被编码,
比如有个url为search.php?term=x & y/z,那么此时不设置B参数时会被编码成search.php?term=x%20&y%2Fz=,也是不对的,那么设置B参数之后会被编码成/search.php?term=x%20%26%20y%2Fz,这样对于url rewrite的规则才能被正确解析转发。
C|chain 如果匹配,会继续匹配下一条规则,如果不匹配则跳过后面所有规则;
CO|cookie 可以给当前url设置cookie,规则如:[CO=NAME:VALUE:DOMAIN:lifetime:path:secure:httponly]
示例:
RewriteEngine On
RewriteRule ^/index\.html - [CO=frontdoor:yes:.example.com:1440:/]
DPI|discardpathinfo 丢弃PATH_INFO 信息
E|env 设置环境变量 [E=VAR:VAL] [E=!VAR]
示例:
RewriteRule \.(png|gif|jpg) - [E=image:1]
CustomLog logs/access_log combined env=!image
F|forbidden 直接返回403状态码
RewriteRule \.exe - [F]
G|gone 返回410 状态码,表示资源不再可用
RewriteRule oldproduct - [G,NC]
H|handler 指定请求被某个handler处理,下面这个url表示被php处理引擎处理
RewriteRule !\. - [H=application/x-httpd-php]
L|last 匹配了就不再匹配后面的规则
RewriteBase /
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*) /index.php?req=$1 [L,PT]
N|next 重新从第一条规则开始匹配
RewriteRule (.*)A(.*) $1B$2 [N]
NC|nocase 大小写不敏感
RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC]
NE|noescape 不转码特殊字符,默认是会把&,?等特殊字符转成16进制编码,
RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]
NS|nosubreq 规则不作用于子请求上, SSI (Server Side Include)
P|proxy 做为反向代理转发请求,这样浏览器url就不会改变
RewriteRule /(.*)\.(jpg|gif|png) http://images.example.com/$1.$2 [P]
PT|passthrough url作为一个文件路径处理
Alias /icons /usr/local/apache/icons
RewriteRule /pics/(.+)\.jpg /icons/$1.gif [PT]
QSA|qsappend 带上query参数
RewriteRule /pages/(.+) /page.php?page=$1 [QSA]
R|redirect 重定向 默认302 重定向
S|skip 跳过不想执行的规则匹配
# Is the request for a non-existent file?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If so, skip these two RewriteRules
RewriteRule .? - [S=2]
RewriteRule (.*\.gif) images.php?$1
RewriteRule (.*\.html) docs.php?$1
T|type 指定MIME type 类型
# Serve .pl files as plain text
RewriteRule \.pl$ - [T=text/plain]
# Files with 'IMG' in the name are jpg images.
RewriteRule IMG - [T=image/jpg]
apache url rewrite 的RewriteRule参数详解的更多相关文章
- 新浪sae url rewrite(伪静态、重定向)详解
新浪sae url rewrite(伪静态.重定向)详解 http://www.veryhuo.com phpclubs 2011-11-14 投递稿件 sae全程Sina App Engine,真是 ...
- jdbc URL中的各个参数详解
常用的有两个,一个是gjt(Giant JavaTree)组织提供的mysql驱动,其JDBC Driver名称(Java类名)为:org.gjt.mm.mysql.Driver 详情请参见网站:ht ...
- mysql:JDBC url 参数详解
MySql链接url参数详解 jdbc:mysql://[host:port],[host:port].../[database][?参数名1][=参数值1][&参数名2][=参数值2]... ...
- Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
转载:http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...
- Nginx反向代理、负载均衡、页面缓存、URL重写及读写分离详解
大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...
- [转载]Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...
- (转)Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
原文:http://blog.51cto.com/freeloda/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Ng ...
- Nginx主配置参数详解,Nginx配置网站
1.Niginx主配置文件参数详解 a.上面博客说了在Linux中安装nginx.博文地址为:http://www.cnblogs.com/hanyinglong/p/5102141.html b.当 ...
- linux PHP 编译安装参数详解
linux PHP 编译安装参数详解 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc -- ...
随机推荐
- DNA Pairing
function pair(str) { //return str; var arr = str.split(''); var pait = ''; var result = arr.map(func ...
- SQL Server 树查询
WITH treeAS(SELECT ParentAssetID, AssetID,1 AS x2level,nodename,CAST(nodename AS NVARCHAR(max)) x2na ...
- Problem 2136 取糖果---FUOJ (线段树+维护)
http://acm.fzu.edu.cn/problem.php?pid=2136 题目大意: 给你n个袋子每个袋子里都装有糖果,然后呢你可以每次抽取一个连续的一个区间的袋子,然后带走里面最多糖果数 ...
- 中值排序的java实现
public class MidSort { public static void main(String[] args){ ,,,,,,,,}; midSort(arr,,); for(int i: ...
- Class类的理解
在java中,每个类都有一个相应的Class类的对象,因为每个类编译完成后,在生成的.class文件中,就会产生一个Class对象. 在运行期间,如果我们要产生某个类的对象,jvm会检查类 ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- 【08_238】Product of Array Except Self
Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...
- 泛型,存放N张图片
(* ************************************************* 1.里面放多张图片,用文件名作为索引 2.线程在背后从硬盘加载到内存 3.批量加载 4.加载完 ...
- don't forget the bigger picture
Imagine a circle that contains all of human knowledge: By the time you finish elementary school, you ...
- jquery给input域赋值和取值
参考: http://blog.csdn.net/happymagic/article/details/8707117