apache rewrite 正则表达式基础,apacherewrite


用了好几次rewrite,用的次数不是很多,每次都忘,都得重新上网上找一堆现看,总结一下,以免以后忘了

=====================分隔符=====================

注意以下几点:

1、加载rewrite模块,LoadModule rewrite_module modules/mod_rewrite.so

2、如果是用vhost里配置,则在vhost里打开RewriteEngine on(htaccess文件配置的方式没有用过)


用过的配置:

场景一:当域名是aaa.bbb的时候,跳转到aaa.ccc.bbb这个域名

RewriteEngine on

RewriteCond %{HTTP_HOST} ^aaa\.bbb$ [NC]
RewriteRule ^/(.*)$  http://aaa.ccc.bbb/$1  [R=301,L,NC]

%{HTTP_HOST}匹配域名,^表示匹配开始,$表示匹配结束,分别是匹配字符串的左右界,可以不同时出现,\.表示转义,只匹配.,否则就变成任意单个字符了

NC(no case) 不区分大小写,

.是任意单个字符,.*就是任意数量的任意字符,()是子字符串,所以^/(.*)$表示域名后任意路径,这里猜测,应该可以省略$,这里的.,没有用\转义,是因为这部分不是正则,$1匹配的是第一个()里的内容,R=301表示永久跳转,


场景二:把/testt/这个请求,转到/image/下,但是/testt/xxxx,不发生跳转

RewriteEngine on
RewriteRule ^/testt/$  http://localhost:8000/image/  [R=301,L,NC]

对于query string的操作,以下引自官方文档

Modifying the Query String

By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.


=====================分隔符=====================

参考资料:

正则表达式中各种字符的含义 http://www.cnblogs.com/afarmer/archive/2011/08/29/2158860.html

apache的rewrite规则使用说明 http://www.jb51.net/article/48780.htm

13个实用的Apache Rewrite重写规则 http://www.jb51.net/article/47907.htm



相关内容

    暂无相关文章